【问题标题】:Google Directory API Access using Java使用 Java 访问 Google Directory API
【发布时间】:2013-10-01 02:01:06
【问题描述】:

我正在尝试使用 Google Admin API 获取和更新我的域的用户

  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
  private static final List<String> SCOPES = Arrays.asList(
      "https://www.googleapis.com/auth/admin.directory.user",
      "https://www.googleapis.com/auth/admin.directory.user.readonly");

  public static void main(String[] args) {
    try {
      HttpTransport httpTransport = new NetHttpTransport();


      GoogleCredential credential =
          new GoogleCredential.Builder()
              .setTransport(httpTransport)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId(
                  "xxxxx-yyyyy@developer.gserviceaccount.com")
              .setServiceAccountUser("sysadmin@mydomain.com")
              .setServiceAccountScopes(SCOPES)
              .setServiceAccountPrivateKeyFromP12File(
                  new File("C:\\privatekey.p12")).build();

      Directory admin =
          new Directory.Builder(httpTransport, JSON_FACTORY, credential)
              .setApplicationName("User Sync Service")
              .setHttpRequestInitializer(credential).build();

      Directory.Users.List list = admin.users().list();
      list.setDomain("mydomain.com");
      Users users = list.execute();
      System.out.println("************");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

我收到了这个错误

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "access_denied"
}
    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 com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)

截图:

【问题讨论】:

  • 如果有什么方法可以直接使用用户名和密码或者其他方式访问API,请贴出来。 :(

标签: java google-api-java-client google-admin-sdk


【解决方案1】:

您看到的具体错误可能意味着您没有在 CPanel 的第 3 方 OAuth 设置中授予服务帐户对 Google Apps 域的访问权限。此步骤在Google Drive domain-wide delegation documentation 中进行了描述(只是目录范围中的子项)。

此外,您可能只想为web serversinstalled applications 使用常规OAuth 2.0 令牌,而不是使用服务帐户。它仍然不像提供管理员用户/通行证那么简单,但它比服务帐户更简单,并且比用户/通行证访问更安全,因为您正在确定访问范围而不是直接接触用户密码。

【讨论】:

  • 这解决了问题。撞了我的头将近1周。谷歌文档非常具有误导性。更好的是应该正确更新
  • 一点帮助。如何在控制台中记录请求和响应。有没有办法记录它们?
  • 但是如果您使用常规的 oauth 令牌而不是服务帐户,则必须在应用程序启动时手动授权,这很不舒服,特别是如果您在 Docker 上运行 Spring Boot 应用程序,这是我的情况。
  • Nico,第一次获取并存储一个刷新令牌。然后,您可以在应用运行时获取访问令牌。 developers.google.com/identity/protocols/oauth2/…
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2019-05-19
  • 2013-09-13
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多