java - Google Contact API (No Authentication Header Information Error) -
i new google contact api. trying retrieve contacts using google contact api. used oauth authentication , google contact api.
import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import com.google.gdata.client.query; import com.google.gdata.client.authn.oauth.googleoauthparameters; import com.google.gdata.client.authn.oauth.oauthhmacsha1signer; import com.google.gdata.client.authn.oauth.oauthparameters.oauthtype; import com.google.gdata.client.contacts.contactsservice; import com.google.gdata.data.link; import com.google.gdata.data.contacts.contactentry; import com.google.gdata.data.contacts.contactfeed; import com.google.gdata.data.contacts.groupmembershipinfo; import com.google.gdata.data.extensions.email; import com.google.gdata.data.extensions.extendedproperty; import com.google.gdata.data.extensions.im; import com.google.gdata.data.extensions.name; import com.google.gdata.util.serviceexception; public class googlecontactsaccess{ /* method authenticate user credentials passed , returns instance of contactservice class.*/ public contactsservice authenticateid(){ googleoauthparameters oauthparameters = null; contactsservice contactservice = null; try{ contactservice = new contactsservice("api project"); oauthparameters = new googleoauthparameters(); oauthparameters.setoauthconsumerkey("consumerkey"); oauthparameters.setoauthconsumersecret("consumerkey"); oauthparameters.setscope("https://www.google.com/m8/feeds"); oauthparameters.setoauthtype(oauthtype.two_legged_oauth); oauthparameters.addcustombaseparameter("xoauth_requestor_id", "my id@gmail.com"); contactservice.setoauthcredentials(oauthparameters,new oauthhmacsha1signer()); contactservice.getrequestfactory().setheader("gdata-version", "3.0"); }catch(exception e){ e.printstacktrace(); } return contactservice; } /* method print details of contacts available in particular google account. */ public void printallcontacts(contactsservice myservice)throws serviceexception, ioexception { // request feed url feedurl = new url("https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json'"); // query retrieve contacts query myquery = new query(feedurl); contactfeed resultfeed = myservice.getfeed(myquery, contactfeed.class); // print results system.out.println(resultfeed.gettitle().getplaintext()); (int = 0; < resultfeed.getentries().size(); i++) { contactentry entry = resultfeed.getentries().get(i); if (entry.hasname()) { name name = entry.getname(); if (name.hasfullname()) { string fullnametodisplay = name.getfullname().getvalue(); if (name.getfullname().hasyomi()) { fullnametodisplay += " (" + name.getfullname().getyomi() + ")"; } system.out.println("\t\t" + fullnametodisplay); } else { system.out.println("\t\t (no full name found)"); } if (name.hasnameprefix()) { system.out.println("\t\t" + name.getnameprefix().getvalue()); } else { system.out.println("\t\t (no name prefix found)"); } if (name.hasgivenname()) { string givennametodisplay = name.getgivenname().getvalue(); if (name.getgivenname().hasyomi()) { givennametodisplay += " (" + name.getgivenname().getyomi() + ")"; } system.out.println("\t\t" + givennametodisplay); } else { system.out.println("\t\t (no given name found)"); } if (name.hasadditionalname()) { string additionalnametodisplay = name.getadditionalname().getvalue(); if (name.getadditionalname().hasyomi()) { additionalnametodisplay += " (" + name.getadditionalname().getyomi() + ")"; } system.out.println("\t\t" + additionalnametodisplay); } else { system.out.println("\t\t (no additional name found)"); } if (name.hasfamilyname()) { string familynametodisplay = name.getfamilyname().getvalue(); if (name.getfamilyname().hasyomi()) { familynametodisplay += " (" + name.getfamilyname().getyomi() + ")"; } system.out.println("\t\t" + familynametodisplay); } else { system.out.println("\t\t (no family name found)"); } if (name.hasnamesuffix()) { system.out.println("\t\t" + name.getnamesuffix().getvalue()); } else { system.out.println("\t\t (no name suffix found)"); } } else { system.out.println("\t (no name found)"); } system.out.println("email addresses:"); (email email : entry.getemailaddresses()) { system.out.print(" " + email.getaddress()); if (email.getrel() != null) { system.out.print(" rel:" + email.getrel()); } if (email.getlabel() != null) { system.out.print(" label:" + email.getlabel()); } if (email.getprimary()) { system.out.print(" (primary) "); } system.out.print("\n"); } system.out.println("im addresses:"); (im im : entry.getimaddresses()) { system.out.print(" " + im.getaddress()); if (im.getlabel() != null) { system.out.print(" label:" + im.getlabel()); } if (im.getrel() != null) { system.out.print(" rel:" + im.getrel()); } if (im.getprotocol() != null) { system.out.print(" protocol:" + im.getprotocol()); } if (im.getprimary()) { system.out.print(" (primary) "); } system.out.print("\n"); } system.out.println("groups:"); (groupmembershipinfo group : entry.getgroupmembershipinfos()) { string grouphref = group.gethref(); system.out.println(" id: " + grouphref); } system.out.println("extended properties:"); (extendedproperty property : entry.getextendedproperties()) { if (property.getvalue() != null) { system.out.println(" " + property.getname() + "(value) = " + property.getvalue()); } else if (property.getxmlblob() != null) { system.out.println(" " + property.getname() + "(xmlblob)= " + property.getxmlblob().getblob()); } } link photolink = entry.getcontactphotolink(); string photolinkhref = photolink.gethref(); system.out.println("photo link: " + photolinkhref); if (photolink.getetag() != null) { system.out.println("contact photo's etag: " + photolink.getetag()); } system.out.println("contact's etag: " + entry.getetag()); } } public static void main(string args[]){ try { googlecontactsaccess googlecontactsaccess = new googlecontactsaccess(); contactsservice contactsrv = googlecontactsaccess.authenticateid(); googlecontactsaccess.printallcontacts(contactsrv); } catch (malformedurlexception ex) { ex.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); }catch (exception ex) { ex.printstacktrace(); } } } i tried above code. getting below exception
java.lang.nullpointerexception: no authentication header information @ com.google.gdata.util.authenticationexception.initfromauthheader(authenticationexception.java:96) @ com.google.gdata.util.authenticationexception.(authenticationexception.java:67) @ com.google.gdata.client.http.httpgdatarequest.handleerrorresponse(httpgdatarequest.java:608) @ com.google.gdata.client.http.googlegdatarequest.handleerrorresponse(googlegdatarequest.java:564) @ com.google.gdata.client.http.httpgdatarequest.checkresponse(httpgdatarequest.java:560) @ com.google.gdata.client.http.httpgdatarequest.execute(httpgdatarequest.java:538) @ com.google.gdata.client.http.googlegdatarequest.execute(googlegdatarequest.java:536) @ com.google.gdata.client.service.getfeed(service.java:1135) @ com.google.gdata.client.service.getfeed(service.java:1077) @ com.google.gdata.client.googleservice.getfeed(googleservice.java:676) @ com.google.gdata.client.service.getfeed(service.java:1034) @ com.cohere.getcontacts.googlecontactsaccess.printallcontacts(googlecontactsaccess.java:59) @ com.cohere.getcontacts.googlecontactsaccess.main(googlecontactsaccess.java:197)
so 1 me resolve problem in advance
it looks using consumerkey both key , secret in code :
oauthparameters.setoauthconsumerkey("consumerkey"); oauthparameters.setoauthconsumersecret("consumerkey"); you need use correct consumersecret of app
Comments
Post a Comment