java - 使用 Java 和 OAuth 2.0 连接到 Google 通讯录

标签 java oauth-2.0 google-oauth google-oauth-java-client

我在使用 OAuth 2.0 通过 Java 连接到 Google Contacts API 时遇到问题。经过大量搜索后,我已经到了可以连接的地步,但在最后的过程中收到错误消息。

我做了以下事情:

1) 通过 Google 的开发者控制台为项目创建了一个 OAuth 2.0 服务帐户。

2) 按照我在 https://developers.google.com/identity/protocols/OAuth2ServiceAccount 找到的“将全域权限委托(delegate)给服务帐户”的步骤进行操作,我已将在上面第 1 步中创建的服务帐户的客户端 ID 授权给范围 https://www.google.com/m8/feeds/对于联系人(读/写)。

3) 这是我根据其他成员的帖子编写的代码:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.client.contacts.ContactsService;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class Connector {
private static ContactsService contactService = null;
private static HttpTransport httpTransport;

private static final String APPLICATION_NAME = "MyProject";
private static final String SERVICE_ACCOUNT_EMAIL = "service-account-email-address@developer.gserviceaccount.com";
private static final java.util.List<String> SCOPE = Arrays.asList("https://www.google.com/m8/feeds");

private Connector() {
    // explicit private no-args constructor
}

public static ContactsService getInstance() {
    if (contactService == null) {
        try {
            contactService = connect();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return contactService;
}

private static ContactsService connect() throws GeneralSecurityException, IOException {
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    java.io.File p12File = new java.io.File("MyProject.p12");

    // @formatter:off
    GoogleCredential credential = new GoogleCredential.Builder()
                                                    .setTransport(httpTransport)
                                                    .setJsonFactory(JacksonFactory.getDefaultInstance())
                                                    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                                                    .setServiceAccountScopes(SCOPE)
                                                    .setServiceAccountPrivateKeyFromP12File(p12File)
                                                    .setServiceAccountUser("user@example.com")
                                                    .build();
    // @formatter:on

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

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

    return myService;
}
}

从理论上讲,我相信这应该一切正常。但是,当执行 credential.refreshToken() 时,我收到错误:

com.google.api.client.auth.oauth2.TokenResponseException: 403 Forbidden
{
  "error" : "access_denied",
  "error_description" : "Requested client not authorized."
}

如果我删除 setServiceAccountUser("user@example.com"),我会收到错误消息 com.google.gdata.util。 ServiceForbiddenException:无法请求属于其他用户的联系人。我相信我需要保留 setServiceAccountUser(...) 但无法通过 token 响应异常。

我不确定从这里到哪里去。

感谢您提供的任何建议或帮助。

最佳答案

对于任何感兴趣或遇到与我遇到的问题相同的人,我都遇到过这个 link这解决了这个问题。主要问题是我在需要使用的范围末尾的“/”。它需要添加到我的代码中以匹配我为服务帐户 API 访问提供的范围。

我的最终代码:

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

String APPLICATION_NAME = "MyProject";
String SERVICE_ACCOUNT_EMAIL = "my-service-account@developer.gserviceaccount.com";
java.io.File p12File = new java.io.File("MyProject.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()) {
    System.out.println("group: " + group.getTitle().getPlainText());

    Query cQuery = new Query(new java.net.URL("https://www.google.com/m8/feeds/contacts/user@example.com/full"));
    cQuery.setMaxResults(32767);
    String grpId = group.getId();
    cQuery.setStringCustomParameter("group", grpId);
    ContactFeed feed = service.query(cQuery, ContactFeed.class);

    for (ContactEntry contact : feed.getEntries()) {
        System.out.println("name: " + contact.getTitle().getPlainText());
    }
}

关于java - 使用 Java 和 OAuth 2.0 连接到 Google 通讯录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511117/

相关文章:

java - 如何创建自定义 Android Listview 标题并将它们分组为几天

javascript - 我应该如何在 Google Picker 和 Google Drive 中使用刷新 token ?

oauth - 得到一个随机错误 : policy_enforced on most Google Apps when using OAuth 2

javascript - 新的 Google 登录服务按钮自定义

java - 计算 future 头寸

java - 在客户端计算机上实现一个小型数据库应用程序

javascript - 无需身份验证即可使用 Youtube API

python - 使用python访问Google API : Refresh Error 'invalid grant' appears suddenly after a few days of first-time setup

java - hibernate 域模型

authentication - 通过 stormpath 进行 Apigee 身份验证。这可行吗?