使用 Google 管理目录 API 添加组 - 403 无权访问此 Resource/API

Adding Group using Google Admin Directory API - 403 Not Authorized to Access this Resource/API

我正在尝试使用 Google 管理目录 API 从 Google App Engine 上 运行 的 Java 应用添加一个组.我在 SO 上查看了许多其他相关问题并尝试了这些建议,但我仍然遇到问题。

我使用 Google 云控制台创建了一个新的服务帐户,并且启用了 G-Suite 全域委派。我已经为服务帐户创建了一个 JSON 密钥。

我在 Google 管理控制台中启用了 Admin Directory SDK,然后为该服务帐户的 'Client ID' 分配了 API 范围Google 安全 > 高级设置 > 管理 API 客户端访问下的管理控制台。我正在分配范围 https://www.googleapis.com/auth/admin.directory.group:

在 Google 管理控制台中,我还在“帐户”>“管理员角色”下将系统管理员帐户设置为群组管理员:

然后,在 Java App Engine 上运行的 Java 应用程序中,我正在执行以下操作以使用服务帐户凭据模拟我已授予该组的超级管理员用户管理员权限:

final CREDENTIALS_FILE_PATH = "path_to_my_JSON_credentials_file"
final USER_EMAIL = "email_address_for_superadmin_with_group_admin_rights"

public someMethod() { 
    Directory directory = getDirectoryService(USER_EMAIL); 

    com.google.api.services.admin.directory.model.Group group = new Group(); 
    group.setEmail("myemail@domain.com"); 
    group.setName("test_group"); 
    group.setDescription("test_group_desc"); 

    Group googleGroup = directory.groups().insert(group).execute(); 
} 

/** 
* Build and returns a Directory service object authorized with the service accounts 
* that act on behalf of the given user. 
* 
* @param userEmail The email of the user. Needs permissions to access the Admin APIs. 
* @return Directory service object that is ready to make requests. 
*/ 
public static Directory getDirectoryService(String userEmail) throws GeneralSecurityException, 
IOException, URISyntaxException { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 

    InputStream resourceAsStream = AdminService.class.getResourceAsStream(CREDENTIALS_FILE_PATH); 
    Collection<String> scopeList = new ArrayList<>(); 
    scopeList.add(DirectoryScopes.ADMIN_DIRECTORY_GROUP); 

    GoogleCredential gcFromJson = GoogleCredential.fromStream(resourceAsStream).createScoped(scopeList); 

    GoogleCredential credential = new GoogleCredential.Builder() 
    .setTransport(gcFromJson.getTransport()) 
    .setJsonFactory(gcFromJson.getJsonFactory()) 
    .setServiceAccountId(gcFromJson.getServiceAccountId()) 
    .setServiceAccountUser(userEmail) 
    .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) 
    .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) 
    .build(); 

    Directory service = new Directory.Builder(httpTransport, jsonFactory, null) 
    .setHttpRequestInitializer(credential).build(); 
    return service; 
}

这一行:

Group googleGroup = directory.groups().insert(group).execute(); 

我收到以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Not Authorized to access this resource/api",
    "reason" : "forbidden"
  } ],
  "message" : "Not Authorized to access this resource/api"
}

我需要做什么来进行身份验证?

现在可以使用了。导致 403 的问题之一是我在这一行中尝试使用与我的 GSuite 域不匹配的域创建一个组:

group.setEmail("myemail@domain.com");

您可能还必须遵循此 Google 指南才能为第三方应用启用访问权限:

Control access to less secure apps