2012-11-25 120 views
0

我使用谷歌API联系为通过Java访问我的联系人。我正在使用我的Gmail帐户。我的帐户有双向密码验证。所以我写了一个示例程序来显示标题。在 com.google.gdata.client.GoogleAuthTokenFactory.getAuthException(GoogleAuthTokenFactory 证书无效:但我收到错误,如螺纹谷歌联系API错误

异常 “主要” com.google.gdata.client.GoogleService $ InvalidCredentialsException。的java:600) 在 com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:500) 在 com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:346) 在 COM .google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:362) 在 com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:317) 在 com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:301) 在com.contact.manager。 Create.createContact(Create.java:15)在 com.contact.manager.Create.main(Create.java:26)

我的代码:

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. 我使用两在那T我的帐户进行一步验证ime我得到这样的错误。
  2. 在使用普通帐户(我的意思是一个一步到位的认证),我没有收到任何类型的错误。

有什么特殊的方式,同时具有两步认证做认证。 你能帮忙解决这个问题吗?我困在这里。

回答

1

随着谷歌的两个步骤文件中明确规定,不能为API访问使用你的常规密码。

相反,您应该使用OAuth 2.
或者,您可以创建应用专用密码。

+0

嗨,感谢您的回复。我不明白什么是应用程序特定的密码。我如何创建该密码。您能否提供如何使用OAuth2身份验证进行身份验证?如果您提供任何示例代码,对我更有帮助。 – Amar

+0

http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833 – SLaks

+0

https://developers.google.com/accounts/docs/OAuth2 – SLaks

1

就试试这个!我希望这会有所帮助。当API请求失败时,HTTP将以4xx或5xx响应代码的形式返回,这些代码一般可识别失败,以及提供有关导致失败的错误的更多特定信息的XML响应。