gpt4 book ai didi

java - OAuth Google API for Java 无法模拟用户

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:21:47 25 4
gpt4 key购买 nike

我想模拟一个用户并代表他们从服务器进程将文件添加到用户 Google 云端硬盘。我已经设置了一个服务帐户,并且可以使用以下代码成功访问驱动器作为服务帐户添加和列出文件等:

  /** 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();
}

这有效,但是只返回与我认为与服务帐户驱动器 (?) 相关联的文件相关联的文件。

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

        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();

但是,执行这段代码时,会抛出如下异常:

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)

我是否缺少步骤或配置设置?

谢谢,大卫

最佳答案

我发现了一个与我类似的问题:Can a Google Apps Admin manage users files with Drive SDK?到我的,这帮助我找到了答案。

cPanel 文档有点误导,因为它提到启用消费者 key ,然后将域添加到管理 API 客户端访问屏幕。这似乎对 gdata api 有效,对新的 Google Drive api 无效。通过按照其他问题中的建议添加客户端 ID 并授予对 Drive 范围的访问权限,我现在可以模拟用户。

关于java - OAuth Google API for Java 无法模拟用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11660666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com