Wednesday, 15 May 2013

java - CORS ~ JQuery ~ XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' -



java - CORS ~ JQuery ~ XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' -

i tired issue

i seek fetch google contact first log in client id , client secret value , scope value like

scope=https://www.google.com/m8/feeds/&response_type=code

and set redirect uri.

at server side have manage headers like

response.setheader("access-control-allow-origin", "*"); response.setheader("access-control-allow-methods", "post, get, options, put, delete, head"); response.setheader("access-control-allow-headers", "x-pingother, origin, x-requested-with, content-type, accept"); response.setheader("access-control-max-age", "1728000");

and jquery function under

function ok(){ $.ajax({ type : 'post', datatype: "json", contenttype:"application/x-www-form-urlencoded; charset=utf-8", crossdomain:true, cache : true, info : { code : '<%=auth_code%>',client_id: '<%=client_id%>', client_secret : '<%=client_secret%>',redirect_uri: '<%=redirect_uri%>', max_results : <%=max_results%>,grant_type:'authorization_code' }, url : 'https://accounts.google.com/o/oauth2/token', success : function(data){ //alert("success "+data['access_token']); var accesstoken = data['access_token']; //alert(accesstoken); var url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=<%=max_results%>&oauth_token='+accesstoken; //alert(url); $.ajax({ url : url, datatype: "xml", type: "get", success : function(data){ alert("success "+data); $(data).find("entry").each(function(){ for(var i=0 ;i<$(this)[0].children.length;i++){ if($(this)[0].children[i].nodename == "gd:email") console.log($(this)[0].children[i].attributes[1].textcontent); } }); }, error: function(jqxhr, exception, errorstr) { console.log(jqxhr); alert(errorstr); } }); }, error: function(jqxhr, exception, errorstr) { console.log(jqxhr); alert(errorstr); } }); }

here issue client side multiple request.

above code execute multiple request in ajax using jquery.

in contrast have nail url @ server side (servlet).

now code like

$.ajax({ type : 'post', datatype: "json", contenttype:"application/x-www-form-urlencoded; charset=utf-8", crossdomain:true, cache : true, info : { code : '<%=auth_code%>' }, url : '<%=request.getcontextpath()%>/getaccesstokenservlet', success : function(data){ alert(data); }, error: function(jqxhr, exception, errorstr) { console.log(jqxhr); alert(errorstr); } });

and servlet "getaccesstokenservlet" like

protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string code = request.getparameter("code"); string grant_type = "authorization_code"; string charset = "utf-8"; string accessparameters = string.format("client_id=%s&client_secret=%s&redirect_uri=%s&max_results=%s&code=%s&grant_type=%s", urlencoder.encode(client_id, charset),urlencoder.encode(client_secret, charset),urlencoder.encode(redirect_uri, charset), urlencoder.encode(max_results, charset),urlencoder.encode(code, charset),urlencoder.encode(grant_type, charset)); string resultstr = ""; try{ url accesstoken_url = new url("https://accounts.google.com/o/oauth2/token"); httpurlconnection accesstokenconnection = (httpurlconnection) accesstoken_url.openconnection(); // treeconnection.setrequestmethod("get"); accesstokenconnection.setdooutput(true); accesstokenconnection.setrequestproperty("content-type", "application/x-www-form-urlencoded;charset=utf-8"); accesstokenconnection.setrequestproperty("accept-charset", charset); seek { accesstokenconnection.getoutputstream().write(accessparameters.getbytes(charset)); } { accesstokenconnection.getoutputstream().close(); } bufferedreader accesstokenreader = new bufferedreader(new inputstreamreader(accesstokenconnection.getinputstream())); string line; while((line = accesstokenreader.readline()) != null){ resultstr+=line; } system.out.println(resultstr); jsonobject jsonobject = new jsonobject(resultstr); string oauth_token = string.valueof(jsonobject.get("access_token")); url contacturl = new url("https://www.google.com/m8/feeds/contacts/default/full?oauth_token="+oauth_token); httpurlconnection contactconnection = (httpurlconnection) contacturl.openconnection(); contactconnection.setrequestmethod("get"); contactconnection.setdooutput(true); contactconnection.setrequestproperty("content-type", "text/xml"); contactconnection.setrequestproperty("pragma", "no-cache"); contactconnection.setrequestproperty("cache-control", "no-cache"); contactconnection.setrequestproperty("connection", "keep-alive"); contactconnection.setrequestproperty("accept-encoding", "gzip, deflate"); system.out.println("length: "+contactconnection.getcontentlength()); system.out.println("type: "+contactconnection.getcontenttype()); bufferedreader contactreader = new bufferedreader(new inputstreamreader(contactconnection.getinputstream())); string contactline; string contactstr = ""; while ((contactline = contactreader.readline()) != null) { contactstr += contactline; //system.out.println("contactline: "+contactline); if(contactline.contains("gd:email")){ string[] gdemail = contactline.split(" "); for(int i=0;i<gdemail.length;i++){ if(gdemail[i].startswith("address")){ system.out.println(gdemail[i].substring(gdemail[i].indexof("=")+1)); } } } } }catch (exception e) { e.printstacktrace(); } response.getwriter().write(resultstr.tostring()); }

this code returns friends email addresses.

java jquery jsp google-api

No comments:

Post a Comment