Search code examples
google-contacts-api

Google Contact API Error


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.


Solution

  • 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.