Google api Google Admin SDK,带有服务帐户,无需模拟

Google api Google Admin SDK,带有服务帐户,无需模拟,google-api,service-accounts,google-admin-sdk,Google Api,Service Accounts,Google Admin Sdk,目标是在G-Suite平台中建立一个服务帐户,应用程序可以使用该帐户执行操作,而无需提示用户进行身份验证 我们遇到的问题与下面的帖子非常相似,但略有不同 只要我们遵循“模拟”流程,事情就会发生。通过模拟,授权规则遵循被模拟的用户。理想情况下,我们希望相应地确定服务帐户的范围,而不是模拟用户。当我们这样做时,我们会不断收到403 我们已尽最大努力遵循以下说明: 我们的Java代码片段与此类似: final NetHttpTransport HTTP_TRANSPORT =

目标是在G-Suite平台中建立一个服务帐户,应用程序可以使用该帐户执行操作,而无需提示用户进行身份验证

我们遇到的问题与下面的帖子非常相似,但略有不同

只要我们遵循“模拟”流程,事情就会发生。通过模拟,授权规则遵循被模拟的用户。理想情况下,我们希望相应地确定服务帐户的范围,而不是模拟用户。当我们这样做时,我们会不断收到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库中,我们启用了管理SDK

在Google管理员中,在安全/高级设置下,我们设置了作用域

当我们运行Java代码时,当“setServiceAccountUser”被注释掉时,我们会看到以下内容(在模拟时可以正常工作,只要用户拥有正确的权限):

所以,我们的SA似乎没有正确连接到作用域,但我不知道如何做到这一点

顺便说一句,105601508644514129999是在SA创建过程中自动生成的OAuth 2.0凭据的SA唯一ID和客户端ID。我们还将SA电子邮件用于“SetServiceAccount”“如以下示例所示,但仍然得到403。实际上,我认为这个例子在.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>

com.google.auth
google-auth-library-oauth2-http
0.20.0
com.google.api
谷歌api服务管理目录
目录1-rev117-1.25.0

有什么想法吗?

服务帐户不在G套件域中(即使它们属于其中的用户/项目),因此无法授予对G套件域的管理员访问权限。他们所能做的就是通过域范围的委派来模拟域中的用户


您可能希望解决的问题是,使用客户机ID和密码将常规OAuth授予管理员用户身份,并确保获得一个长寿命的刷新令牌
access\u type=offline
。这将允许您在访问令牌过期时刷新它,并继续充当单一管理员用户,只要他们不撤销您的访问。

一个小更新。在尝试获取Chrome设备时,我添加了以下代码:ChromeOsDevices chromedevices=dir.ChromeOsDevices().list(“myCustomerId”).execute();当然,我最初收到的是403。我必须在Java代码中为服务帐户和安全API访问添加作用域,并为我模拟的人添加“服务管理员”角色。我相信我已经尝试过各种组合,在适当设置范围和角色之前,请求都没有成功。看起来这就是问题的答案。你能确认一下吗?
        <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>