[英]Accessing users account with service account
我们需要使用 API 来验证某个用户是否作为托管帐户存在(这意味着,它属于我们的 Google Domain 组织)。
GSuite adminSDK 执行该操作,但是,它需要 OAuth2 身份验证,由经过身份验证的用户授权 - https://developers.google.com/admin-sdk/reports/v1/guides/authorizing 。
我的问题是,是否有任何方法可以将 API 与服务帐户一起使用,或者是否有任何其他方法可以通过服务帐户检索此信息,因为它将用于 Server-2-Server 场景。
谢谢,瓦斯科
由于这完全不明显 - 文档对此进行了“提示”,但不要详细说明,我很难找到任何有效的具体示例。 他们都不断返回这个错误:
Google.GoogleApiException: 'Google.Apis.Requests.RequestError
Not Authorized to access this resource/api [403]
Errors [
Message[Not Authorized to access this resource/api] Location[ - ] Reason[forbidden] Domain[global]
]
服务帐户必须模拟另一个用户的基本问题。 它在底部的这个链接中提到,以蓝色突出显示:
https://developers.google.com/admin-sdk/directory/v1/guides/delegation
只有有权访问 Admin API 的用户才能访问 Admin SDK Directory API,因此您的服务帐户需要模拟这些用户之一来访问 Admin SDK Directory API。 此外,用户必须至少登录一次并接受 Google Workspace 服务条款。
但它只是没有点击我应该如何做到这一点 - 这是隐藏在其中一个管理控制台中的一些设置吗? 不 - 您将其作为初始连接的一部分传递。
所以只是为了将说明放在一个地方,并希望避免其他人同样的头痛,这些是步骤:
然后我创建了一个 .NET Core 控制台应用程序并安装了这些 NuGet 包:
这是一个丑陋的概念证明,一切正常:
using System;
using System.IO;
using System.Net;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
namespace GoogleDirectoryTest
{
class Program
{
static void Main(string[] args)
{
var dirService = GetDirectoryService(@"C:\tmp\google-cred-sample-12345abdef.json", "user-with-admin-permission-here@mydomainhere.com", "My App Name");
var list = dirService.Users.List();
list.Domain = "mydomainhere.com";
var users = list.Execute();
foreach (var user in users.UsersValue)
{
Console.WriteLine($"{user.Name.FullName}");
}
Console.ReadKey();
}
static DirectoryService GetDirectoryService(string keyfilepath, string impersonateAccount, string appName)
{
using (var stream = new FileStream(keyfilepath, FileMode.Open, FileAccess.Read))
{
var credentials = GoogleCredential.FromStream(stream).CreateWithUser(impersonateAccount);
if (credentials.IsCreateScopedRequired)
credentials = credentials.CreateScoped(new[] { DirectoryService.Scope.AdminDirectoryUserReadonly });
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = appName,
});
return service;
}
}
希望这可以为其他人省去一些麻烦。
您可能知道,服务帐户不属于单个最终用户,而是属于应用程序。 但是,G Suite 域的管理员可以授权服务帐户访问用户数据,即冒充域中的用户。 这称为全域委托。
为此,请转到管理控制台并按照此处指定的步骤操作。
服务帐户是应用程序而非个人使用的一种特殊帐户。
您可以使用服务帐号访问数据或由机器人帐号本身执行操作,或者代表 Google Workspace 或 Cloud Identity 用户访问数据。
需要一个 Google Cloud 项目才能使用 Google Workspace API 并构建 Google Workspace 插件或应用。 如果您还没有谷歌云项目,请参考: 如何创建谷歌云项目
在使用 Google API 之前,您需要在 Google Cloud 项目中启用它们。
启用 Google Workspace API 参考:如何启用 Google Workspace API
对于此示例,您将使用数据 scope /auth/admin.directory.user.readonly
启用Admin SDK Directory API。
创建服务账号参考: 如何创建服务账号?
在Domain wide delegation
窗格中, select Manage Domain Wide Delegation
。
下载 p12 文件包含您的服务帐户的私钥。
在Google Workspace域开启API访问,参考: 如何开启API访问
要代表 Google Workspace 组织中的用户调用 API,您的服务帐号需要在 Google Workspace 管理控制台中由超级管理员帐号授予域范围的授权
在Google Workspace域中进行全域授权,请参考: 如何向服务账号进行全域授权
Install-Package Google.Apis.Auth
Install-Package Google.Apis.Admin.Directory.directory_v1
完整示例位于GitHub
/// <summary>
/// Example how to list all users from google workspace domain, using a service account (user impersonation).
/// </summary>
internal class Program {
static void Main(string[] args) {
// Scope for only retrieving users or user aliases.
string[] _scopes = {
"https://www.googleapis.com/auth/admin.directory.user.readonly"
};
var _paramters = new SACInitializeParameters(
// The service account ID (typically an e-mail address like: *@*iam.gserviceaccount.com)
serviceAccountId: "[Service Account ID]",
// The full path; name of a certificate file
x509CertificateFilePath: "[X509 Certificate File]",
// The email address of the user you trying to impersonate
impersonateEmail: "[User Email]",
// The scopes which indicate API access your application is requesting
scopes: _scopes);
using (var directoryService = DirectoryServiceFactory.CreateDirectoryService(_paramters)) {
// Retrieves a paginated list of either deleted users or all users in a domain.
var request = directoryService.Users.List();
// The unique ID for the customer's Google Workspace account
// the `my_customer` alias represent current identety account's
request.Customer = "my_customer";
request.MaxResults = 10;
var response = request.Execute();
foreach (var user in response.UsersValue) {
System.Console.WriteLine($"{user.Name.FullName}, {user.PrimaryEmail}, {user.Id}");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.