OAuth Google API for Java无法模拟用户 [英] OAuth Google API for Java unable to impersonate user

查看:214
本文介绍了OAuth Google API for Java无法模拟用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想冒充用户,并以服务器进程的名义向用户Google Drive添加文件。我已经设置了一个服务帐户,并可以使用以下代码成功访问云端硬盘:服务帐户添加和列出文件等。

I would like to impersonate a user and add files to the users Google Drive on their behalf from a server process. I've setup a service account and can successfully access the Drive as the service account adding and listing files, etc. using the following code:

  /** Global instance of the HTTP transport. */
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();

  public static void main(String[] args) {
    try {
        GoogleCredential credential = 
                new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
                  .setJsonFactory(JSON_FACTORY)
                  .setServiceAccountId("XXXXX@developer.gserviceaccount.com")
                  .setServiceAccountScopes(DriveScopes.DRIVE)
                  .setServiceAccountPrivateKeyFromP12File(new File("c:/junk/key.p12"))
                  .build();
        Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();         
        drive.files().list().execute();
  } catch (Exception e) {
     e.printStackTrace();
  }

这个方法可行,但只返回与我假设关联的文件与服务帐户驱动器(?)。

This works, however only returns files that are associated to what I assume is associated with the service accounts drive (?).

根据JavaDoc,GoogleCredential也可用于通过添加服务帐户用户电子邮件地址来模拟用户,如下所示:

According to the JavaDoc, GoogleCredential can also be used to impersonate a user by adding the service account users email address as follows:

        GoogleCredential credential = 
                new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
                  .setJsonFactory(JSON_FACTORY)
                  .setServiceAccountId("XXXXX@developer.gserviceaccount.com")
                  .setServiceAccountScopes(DriveScopes.DRIVE)
                  .setServiceAccountPrivateKeyFromP12File(new File("c:/junk/key.p12"))
                  .setServiceAccountUser("usera@domain.com") //<-- impersonate user a
                  .build();

但是,执行此代码时,会引发以下异常:

However, when executing this code, the following exception is thrown:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "access_denied"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:340)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:508)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:260)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:796)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:198)
at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:237)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)
at com.google.api.services.drive.Drive$Files$List.execute(Drive.java:1071)

我缺少一个步骤或配置设置吗?

Am I missing a step or configuration setting?

谢谢,
David

Thanks, David

推荐答案

我发现了类似于我的问题:,以帮助我找出答案。

I found a similar question as mine: Can a Google Apps Admin manage users files with Drive SDK? to mine which has helped me figure out the answer.

cPanel文档有点误导性,因为它指的是启用使用者密钥,然后将该域添加到Manage API客户端访问屏幕。这似乎对gdata api有效,而不适用于新的Google Drive api。通过添加其他问题中建议的客户端ID并授予对云端硬盘作用域的访问权限,我现在可以模拟用户。

The cPanel documentation is a little misleading as it refers to enabling the consumer key and then adding the domain to the Manage API client access screen. This appears to be valid for the gdata api and not the new Google Drive api. By adding the client id as suggested in the other question and granting access to the Drive scope I'm now able to impersonate a user.

这篇关于OAuth Google API for Java无法模拟用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆