我正在创建一个应该能够读取域的所有用户的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("<admin-account>@<domain>")
...
有用!
但我怎么知道管理员的电子邮件地址?我可以在没有电子邮件地址的情况下工作吗?
不可以。您必须拥有并知道允许执行您想要的帐户的电子邮件地址(通常是superadmin)。
我同意某些API要求这样做(管理员,目录等)是非常荒谬的。
在尝试确定用户是否为域管理员时,我们遇到了完全相同的“麻烦”。我们使用与您相同的范围,类似的代码(在PHP中)与您的相同:
$email = '[email protected]';
$config = array(
'key' => "path/to/private/key",
'name' => "[email protected]",
'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
但是,这有两个限制,这意味着我不能从我的特定用例中受益。
我可以忍受“暂停”的限制,但由于我无法获得没有用户的用户列表,这对我没有帮助。