我是否需要 G Suite 帐户才能使用服务帐户冒充用户?

Do I need G Suite account to make requests impersonating an user with a service account?(我是否需要 G Suite 帐户才能使用服务帐户冒充用户?)
本文介绍了我是否需要 G Suite 帐户才能使用服务帐户冒充用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用服务帐户向 Drive API 发出模拟用户的请求,但我收到 TokenResponseException: 401 Unauthorized 异常.

I'm trying to make requests to Drive API impersonating an user using a service account but I'm getting a TokenResponseException: 401 Unauthorized exception.

我已遵循 https://developers.google.com/identity 中的描述/protocols/OAuth2ServiceAccount:

  • 在 https://console.developers.google.com 中,创建了一个 Project > Credentials >服务帐号 > P12 文件;
  • 启用 Drive API;
  • 启用域范围的委派;
  • 在 https://console.developers.google.com/iam-admin/iam,添加了 Project > Editor 权限以通过电子邮件发送 test@gmail.com(示例).
  • In https://console.developers.google.com, created a Project > Credentials > Service account > P12 file;
  • Enabled Drive API;
  • Enabled domain-wide delegation;
  • In https://console.developers.google.com/iam-admin/iam, added Project > Editor permission to email test@gmail.com (example).

然后,在我的代码中:

File p12 = new File("p12FileFromDevelopersConsole.p12");
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

GoogleCredential c = new GoogleCredential.Builder()
    .setTransport(transport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId("MY_ID@MY-ID.iam.gserviceaccount.com")
    .setServiceAccountUser("test@gmail.com")
    .setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE, DriveScopes.DRIVE_APPDATA, DriveScopes.DRIVE_FILE, DriveScopes.DRIVE_METADATA_READONLY))
    .setServiceAccountPrivateKeyFromP12File(p12)
    .build();

List<com.google.api.services.drive.model.File> files = drive.files()
    .list()
    .setSpaces("drive")
    .setFields("nextPageToken, files(id, name, webViewLink)")
    .setPageSize(10)
    .execute() // < --- exception is thrown here
    .getFiles();

for(com.google.api.services.drive.model.File f : files) {
    System.out.println(f.getName());
}

堆栈跟踪是:

com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
    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:384)
    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:868)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

我注意到,如果我注释掉 .setServiceAccountUser("test@gmail.com") 行,我可以执行代码并在我的 中打印Getting started"System.out.println.

I could note that if I comment out the line .setServiceAccountUser("test@gmail.com"), I am able to execute code and it prints "Getting started" in my System.out.println.

我看过很多关于这个问题的帖子,但没有一个具体的解决方案.然后,我查看了上面引用的文档,其中多次提到 G Suite,然后我意识到这些请求可能只适用于 G Suite 帐户.我对吗?如果没有,我怎样才能让它工作?

I've seen many posts with this problem but none with a concrete solution. Then, I looked at documentation referenced above and it says many times about G Suite and then I realize it could be that these requests only work with a G Suite account. Am I right? If not, how can I make it work?

推荐答案

是的,服务帐户的域范围委派仅适用于 GSuite 用户,因为 GSuite 管理员必须授予服务帐户域范围的权限——请参见此处的步骤 4:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

Yes, domain-wide delegation for service accounts is only for GSuite users because GSuite admins must grant service accounts domain-wide authority -- see Step 4 here: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

考虑使用常规 OAuth (https://developers.google.com/identity/protocols/OAuth2WebServer) 以从您的 gmail 帐户授予对 Web 应用程序的访问权限.

Consider using regular OAuth (https://developers.google.com/identity/protocols/OAuth2WebServer) to grant access to your web app from your gmail account.

这篇关于我是否需要 G Suite 帐户才能使用服务帐户冒充用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)