Google Apps Admin API:需要为服务帐户指定帐户用户吗?

我正在创建一个应该能够读取域的所有用户的Marketplace应用程序。 我请求访问这些范围:

https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/admin.directory.user.readonly 

然后当访问“通用导航扩展”时,会发生这种情况:

 Credential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(SERVICE_MAIL) .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY)) .setServiceAccountPrivateKey(privateKey) .build(); Directory oauth2 = new Directory.Builder(httpTransport, jsonFactory, null) .setHttpRequestInitializer(credential) .build(); Directory.Users.List list = oauth2.users().list(); list.setDomain(queryParams.getString("domain")); Users users = list.execute(); 

当我运行它时,它返回此错误:

 { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Not Authorized to access this resource/api", "reason" : "forbidden" } ], "message" : "Not Authorized to access this resource/api" } 

但是,当我添加:

 Credential credential = new GoogleCredential.Builder() .setServiceAccountUser("@") ... 

有用!

但我怎么知道管理员的电子邮件地址? 我可以在没有电子邮件地址的情况下工作吗?

不可以。您必须拥有并知道允许执行您想要的帐户的电子邮件地址(通常是superadmin)。

我同意某些API要求此(管理员,目录等)是非常荒谬的。

在尝试确定用户是否为域管理员时,我们遇到了完全相同的“麻烦”。 我们使用与您相同的范围,类似的代码(在PHP中)与您的相同:

 $email = 'an_user_from_not_our_domain@domain.com'; $config = array( 'key' => "path/to/private/key", 'name' => "xxxxxxxxxxxxxxx@developer.gserviceaccount.com", 'prn' => "app_domain_admin@app_domain.com" ); $isAdmin = false; $key = file_get_contents($config['key']); $scopes_ = array("https://www.googleapis.com/auth/admin.directory.user.readonly"); $gac = new Google_AssertionCredentials($config['name'], $scopes_, $key, 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', $config['prn']); $client = new Google_Client(); $client->setApplicationName("Test App"); $client->setUseObjects(true); $client->setAssertionCredentials($gac); require_once 'GoogleApiClient/contrib/Google_DirectoryService.php'; $directory = new Google_DirectoryService($client); $user = $directory->users->get($email); if ($user instanceof Google_User) $isAdmin = $user->getIsAdmin(); 

当我们仅为我们自己的域安装应用程序时 – 我们可以获得“isAdmin”值。 当我们将应用程序安装到其他域时,它不起作用并返回相同的“未授权访问此资源/ api”。

也许它适用于Directory API的设计目的? 我的意思是,使用我们自己的域而不是安装我们的应用程序的域。 不推荐使用的Provisioning API允许获取此类信息。 现在我们无法确定用户是否是域管理员。

几天前,Google现在允许任何用户列出域中的用户(也包括子域)。

http://googleappsupdates.blogspot.se/2014/09/new-features-in-admin-sdk-custom-user.html

但是,这有两个限制,这意味着我不能从我的特定用例中受益。

  1. 您仍然需要(活动)用户名来发出请求,如果您无法列出用户,您将如何获得该用户名? 我可能有注册用户名,但该帐户可能随时被删除。 仍然抓到22。

  2. 如果您使用的是非管理员帐户,则必须将viewType设置为“domain_public”,这意味着您只能看到可用字段的子集。 “暂停”字段不是其中之一,您也不会在响应中获得任何暂停的用户。 (此处提供的文档: https : //developers.google.com/admin-sdk/directory/v1/reference/users/list )

我可以忍受“暂停”限制,但由于我无法获得没有用户的用户列表,这对我没有帮助。