目标是在Service平台上建立一个服务帐户,应用程序可以使用该帐户执行操作,而无需提示用户进行身份验证。
我们正在经历的问题非常类似于下面的帖子,但略有不同。
只要我们遵循“模拟”流程,事情就会正常工作。通过模拟,授权规则会跟随被冒充的用户。理想情况下,我们希望相应地限定服务帐户的范围,而不是模拟用户。当我们这样做时,我们经常收到403。
我们已经尽了最大的努力遵循下面的说明:
我们的Java片段类似于以下内容:
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
File file = new File(MdmResource.class.getClassLoader().getResource("myproject.p12").getFile());
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("105601508644514129999")
.setServiceAccountPrivateKeyFromP12File(file)
.setServiceAccountScopes(
Collections.singletonList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY))
.setServiceAccountUser("user@domain.com") // 403 when this is commented out
.build();
Directory dir = new Directory(HTTP_TRANSPORT, JSON_FACTORY, credential);
Get result = dir.users().get("dude@domain.com");
User user = result.execute();
System.out.println(user.get("customerId"));在GCP中,我们在作为超级管理员登录时创建了ServiceAccount。我们启用了域范围的委托并生成了键(在本例中,是p12类型,并将相应的文件用作Java的输入)。
然后,在API中,我们启用了Admin 。
在Google中,在安全/高级设置下,我们设置了作用域。我们使用105601508644514129999和https://www.googleapis.com/auth/admin.directory.device.mobile,https://www.googleapis.com/auth/admin.directory.user.readonly,https://www.googleapis.com/auth/admin.directory.user
当我们运行Java代码时,当"setServiceAccountUser“被注释掉时,我们会看到以下内容(模拟时工作正常,只要用户有正确的权限):
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Not Authorized to access this resource/api",
"reason" : "forbidden"
} ],
"message" : "Not Authorized to access this resource/api"
}因此,我们的SA似乎没有正确地连接到作用域,但我没有办法做到这一点。
顺便说一下,105601508644514129999是SA创建过程中自动生成的OAuth 2.0凭据的SA唯一ID和客户ID。我们还为"setServiceAccountId“使用了SA电子邮件,如下面的示例所示,但仍然得到了403。https://developers.google.com/admin-sdk/directory/v1/guides/delegation。实际上,我认为这个例子对.setServiceAccountScopes也有另一个问题。
终于..。
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.20.0</version>
</dependency>
<!-- Used to navigate Google Directory services, like https://developers.google.com/admin-sdk/directory -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-admin-directory</artifactId>
<version>directory_v1-rev117-1.25.0</version>
</dependency>有什么想法吗?
发布于 2020-03-07 13:27:55
服务帐户不在G套件域中(即使它们是用户/项目拥有的),因此不能授予G Suite域的管理员访问权限。他们所能做的就是用域范围的委托来模拟域中的用户。
您可能希望解决的问题是使用客户端ID和机密将常规OAuth授予管理用户,并确保获得长寿命的刷新令牌access_type=offline。这将允许您在访问令牌到期时刷新它,并继续充当单个管理用户,只要它们不撤销您的访问权限。
https://stackoverflow.com/questions/60550929
复制相似问题