【问题标题】:Adding Group using Google Admin Directory API - 403 Not Authorized to Access this Resource/API使用 Google Admin Directory API 添加组 - 403 未授权访问此资源/API
【发布时间】:2019-05-19 11:40:18
【问题描述】:

我正在尝试通过在 Google App Engine 上运行的 Java 应用使用 Google Admin Directory API 添加组。我查看了许多其他关于 SO 的相关问题并尝试了这些建议,但我仍然遇到问题。

我使用 Google Cloud 控制台创建了一个新服务帐号,并启用了 G-Suite 域范围委派。我已经为服务帐号创建了一个 JSON 密钥。

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

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

然后,在 Google 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"
}

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

【问题讨论】:

    标签: java authentication google-api google-cloud-platform google-admin-sdk


    【解决方案1】:

    它现在正在工作。导致 403 的问题之一是我试图使用与此行中的 GSuite 域不匹配的域创建组:

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

    您可能还必须按照此 Google 指南启用第三方应用的访问权限:

    Control access to less secure apps

    【讨论】:

    猜你喜欢
    • 2016-12-24
    • 2018-05-16
    • 2019-04-16
    • 2017-03-16
    • 2021-03-29
    • 2013-12-17
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多