简体   繁体   English

如何开始使用OAuth2 for Google Contacts API

[英]How to get started with OAuth2 for Google Contacts API

I am trying to make use of Google Contacts API with OAuth 2 authentication. 我正在尝试通过OAuth 2身份验证使用Google Contacts API。 But some of the old sample codes are now not working for this API.I found a sample code which is makes request for Access Token and in response Google servers would give me the Access Token for ~1 hour limit and a Refresh Token with. 但是一些旧的示例代码现在无法使用此API。我发现了一个示例代码,该示例代码请求访问令牌,并且作为响应,谷歌服务器会给我1小时左右的访问令牌和一个刷新令牌。 BUT this code has some issue 但是这段代码有问题

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            String APPLICATION_NAME = "PROJECT_NAME";
            String SERVICE_ACCOUNT_EMAIL = "NUMERCALS-ALPHANUMERICALS@developer.gserviceaccount.com";
            java.io.File p12File = new java.io.File("PROJECT_NAME-NUMERICALS.p12");

            GoogleCredential credential = 
                    new GoogleCredential.Builder()                                                                    
                  .setTransport(httpTransport)                                                     
                  .setJsonFactory(jsonFactory)                                                   
                  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)   
                  .setServiceAccountScopes(
                        Collections.singleton("https://www.google.com/m8/feeds"))                                                          
                  .setServiceAccountPrivateKeyFromP12File(p12File)                                                  
                  .setServiceAccountUser("user@example.com")
                  .build();

            if (!credential.refreshToken()) {
                throw new RuntimeException("Failed OAuth to refresh the token");
            }

            ContactsService service = new ContactsService(APPLICATION_NAME);
            service.setOAuth2Credentials(credential);

            Query gQuery = new Query(new java.net.URL("https://www.google.com/m8/feeds/groups/user@example.com/full"));
            gQuery.setMaxResults(32767);
            ContactGroupFeed groupFeed = service.query(gQuery, ContactGroupFeed.class);

            for (ContactGroupEntry group : groupFeed.getEntries()) {

And I am getting some issues with it 我遇到了一些问题

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at javaCode.FinalCode.main(FinalCode.java:68)

I am new to use OAuth2 for Google APIs so guide me if I am mistaken somewhere or provide a working code snippet.It would be great help.Thanks in advance. 我是第一次使用OAuth2用于Google API,所以如果我在某个地方出现错误或提供了有效的代码段,请为我提供指导。这将是非常有用的帮助。

In you example you are trying to use Service account to impersonate the user. 在您的示例中,您尝试使用服务帐户来模拟用户。 While this is possible, it can only be achieved under Google for work or Google for education accounts. 尽管这是可行的,但只能在Google for Work或Google for Education帐户下实现。 It would not work for normal gmail accounts. 它不适用于普通的Gmail帐户。

If this is the case for your use case, then remember that the domain administrator should grant access to your application and also should grant Domain wide delegation of authority . 如果您的用例是这种情况,请记住,域管理员应授予对您的应用程序的访问权限,还应授予域范围的授权

But if you are just trying to make some tests with the API, then it would be better if you use normal OAuth. 但是,如果您只是想使用API​​进行一些测试,那么使用常规OAuth会更好。

Check this tutorial , I know is for Drive, but there you can see the basic steps to get a authorization token. 查看本教程 ,我知道该教程适用于云端硬盘,但是您可以在其中看到获取授权令牌的基本步骤。 After you get it, the call to the different Google Apis is very similar. 收到后,到其他Google Apis的呼叫非常相似。

I would recommend to check the OAuth Playground . 我建议检查OAuth Playground There you can check the complete OAuth process and you can play with the API. 在那里,您可以检查完整的OAuth流程,并且可以使用API​​。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM