Java Google联系人API(无身份验证标头信息错误)
我是谷歌联系人Api的新手。我正在尝试使用谷歌联系人API检索我的所有联系人。为此,我使用了Oauth身份验证和GoogleContactAPIJava Google联系人API(无身份验证标头信息错误),java,oauth-2.0,google-contacts-api,Java,Oauth 2.0,Google Contacts Api,我是谷歌联系人Api的新手。我正在尝试使用谷歌联系人API检索我的所有联系人。为此,我使用了Oauth身份验证和GoogleContactAPI 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.GoogleO
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{
/* This method will authenticate the user credentials passed to it and returns an 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;
}
/* This method will print details of all the contacts available in that particular Google account. */
public void printAllContacts(ContactsService myService)throws ServiceException, IOException {
// Request the feed
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json'");
// The query that will retrieve all contacts
Query myQuery = new Query(feedUrl);
ContactFeed resultFeed = myService.getFeed(myQuery, ContactFeed.class);
// Print the results
System.out.println(resultFeed.getTitle().getPlainText());
for (int i = 0; i < 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:");
for (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:");
for (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:");
for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) {
String groupHref = group.getHref();
System.out.println(" Id: " + groupHref);
}
System.out.println("Extended Properties:");
for (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();
}
}
}
import java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入com.google.gdata.client.Query;
导入com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
导入com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
导入com.google.gdata.client.authn.oauth.OAuthParameters.OAuthType;
导入com.google.gdata.client.contacts.contacts服务;
导入com.google.gdata.data.Link;
导入com.google.gdata.data.contacts.ContactEntry;
导入com.google.gdata.data.contacts.ContactFeed;
导入com.google.gdata.data.contacts.GroupMembershipInfo;
导入com.google.gdata.data.extensions.Email;
导入com.google.gdata.data.extensions.ExtendedProperty;
导入com.google.gdata.data.extensions.Im;
导入com.google.gdata.data.extensions.Name;
导入com.google.gdata.util.ServiceException;
公共类GoogleContactsAccess{
/*此方法将验证传递给它的用户凭据,并返回ContactService类的实例*/
公共联系人服务认证ID(){
GoogleOAuthParameters oAuthParameters=null;
ContactsService contactService=null;
试一试{
contactService=新的ContactsService(“API项目”);
oAuthParameters=新的GoogleOAuthParameters();
setOAuthConsumerKey(“ConsumerKey”);
setOAuthConsumerCret(“ConsumerKey”);
oAuthParameters.setScope(“https://www.google.com/m8/feeds");
setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
oAuthParameters.addCustomBaseParameter(“xoauth\u请求者\u id”,“我的ID@gmail.com");
setOAuthCredentials(oAuthParameters,新的OAuthHmacSha1Signer());
contactService.getRequestFactory().setHeader(“GData版本”、“3.0”);
}捕获(例外e){
e、 printStackTrace();
}
退货服务;
}
/*此方法将打印该特定Google帐户中所有可用联系人的详细信息*/
public void printAllContacts(contacts服务myService)引发ServiceException,IOException{
//请求提要
URL feedUrl=新URL(“https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json'");
//将检索所有联系人的查询
Query myQuery=新查询(feedUrl);
ContactFeed resultFeed=myService.getFeed(myQuery,ContactFeed.class);
//打印结果
System.out.println(resultFeed.getTitle().getPlainText());
for(int i=0;ioAuthParameters.setOAuthConsumerKey("ConsumerKey");
oAuthParameters.setOAuthConsumerSecret("ConsumerKey");