Google Contact API Error

房东的猫 提交于 2019-12-13 07:09:09

问题


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();
    }
}
  1. I am using Two step authentication for my account at that time I am getting error like this.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!