问题
I am using Google Contact API for accessing my contacts through java. I am using my gmail account for this. My account has Two-way password authentication. So I wrote one sample program to display the title. But I am getting error like
Exception in thread "main" com.google.gdata.client.GoogleService$InvalidCredentialsException: Invalid credentials at com.google.gdata.client.GoogleAuthTokenFactory.getAuthException(GoogleAuthTokenFactory.java:600) at com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:500) at com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:346) at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:362) at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:317) at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:301) at com.contact.manager.Create.createContact(Create.java:15) at com.contact.manager.Create.main(Create.java:26)
My Code :
public class Create {
public void createContact() throws IOException, ServiceException {
ContactsService contactsService = new ContactsService(
"mine-MyProduct-3");
contactsService.setUserCredentials(username,
password);
URL feedUrl = new URL(
"https://www.google.com/m8/feeds/contacts/{EmailID}/full");
ContactFeed resultedFeed = contactsService.getFeed(feedUrl,
ContactFeed.class);
System.out.println(resultedFeed.getTitle().getPlainText());
}
public static void main(String[] args) throws IOException, ServiceException {
Create create = new Create();
create.createContact();
}
}
- I am using Two step authentication for my account at that time I am getting error like this.
- While using normal account (I mean one step authentication) I am not getting any kind of error.
Is there any special way to do authentication while having two step authentication. Can you please help on this. I stuck over here.
回答1:
As Google clearly states in the 2-step documentation, you cannot use your regular password for API access.
Instead, you should use OAuth 2.
Alternatively, you could create an app-specific password.
回答2:
Just try this! I hope it will help a little. When an API request fails, an HTTP will return in 4xx or 5xx response code that generically identifies the failure as well as an XML response that provides more specific information about the errors that caused the failure.
来源:https://stackoverflow.com/questions/13551525/google-contact-api-error