使用服务帐户进行 Google Admin SDK 身份验证

And*_*ias 3 google-api service-accounts google-admin-sdk google-directory-api google-cloud-platform

我的团队目前正在开发一个应用程序,以使用 Admin SDK 在 GCP 中列出我公司的域用户,以用于入职和离职目的。

我们使用服务帐户来执行此操作,并且已admin.directory.user.readonly在 Google 管理员的高级设置中添加了范围。Admin SDK API 已激活,我们可以在 Credentials 区域中看到服务帐户。

当我们使用参数和 调用https://www.googleapis.com/admin/directory/v1/usersviewType=domain_public端点并使用oauth2ldomain=[our domain]生成的访问令牌时,我们会收到以下消息:

{
   "error": {
       "errors": [
           {
               "domain": "global",
               "reason": "forbidden",
               "message": "Not Authorized to access this resource/api"
           }
       ],
       "code": 403,
       "message": "Not Authorized to access this resource/api"
   }
}
Run Code Online (Sandbox Code Playgroud)

是否存在我们没有预见到的任何域限制?

Jay*_*Lee 7

该服务帐户无权为您的 G Suite 实例调用 Directory APi。它有权执行的操作是在您授予其访问权限的 Directory API 范围的上下文中充当您域中的用户。

获取服务帐户凭据时,您需要添加 sub=admin@yourdomain.com 参数,以便您充当域管理员,而不是服务帐户。看:

https://developers.google.com/identity/protocols/OAuth2ServiceAccount

以及一些代码示例:

https://developers.google.com/admin-sdk/directory/v1/guides/delegation