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();
}
}
Is there any special way to do authentication while having two step authentication. Can you please help on this. I stuck over here.
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.