使用Google Admin Directory API添加组-未授权403访问此资源/ API

时间:2018-12-19 01:41:05

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

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

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

enter image description here

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

enter image description here

在Google管理控制台中,我还通过“帐户”>“管理员角色”将系统管理员帐户设为网上论坛管理员:

enter image description here

然后,在运行于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"
}

我需要做什么身份验证?

1 个答案:

答案 0 :(得分:2)

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

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

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

Control access to less secure apps