【问题标题】:Accessing users account with service account使用服务帐户访问用户帐户
【发布时间】:2020-05-13 01:00:18
【问题描述】:

我们需要使用 API 来验证某个用户是否作为托管帐户存在(这意味着该用户属于我们的 Google 域组织)。

GSuite adminSDK 执行该操作,但是,它需要 OAuth2 身份验证,由经过身份验证的用户 - https://developers.google.com/admin-sdk/reports/v1/guides/authorizing 授权。

我的问题是,是否有任何方法可以将该 API 与服务帐户一起使用,或任何其他方法来使用服务帐户检索此信息,因为它将用于 Server-2-Server 场景。

谢谢, 瓦斯科

【问题讨论】:

标签: google-cloud-platform google-admin-sdk


【解决方案1】:

由于这完全不明显 - 文档“暗示”了这一点,但不要拼写出来,我很难找到任何有效的具体示例。他们都一直返回这个错误:

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 包:

  • Google.Apis
  • Google.Apis.Auth
  • Google.Apis.Admin.Directory.directory_v1

这是一个丑陋的概念证明,一切正常:

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;
        }
    }

希望这可以为其他人省去一些麻烦。

【讨论】:

    【解决方案2】:

    您可能知道,服务帐户不属于单个最终用户,而是属于应用程序。但是,G Suite 域的管理员可以授权服务帐户访问用户数据,即模拟域中的用户。这称为域范围的委派

    为此,请转到管理控制台并按照步骤specified here

    参考:

    【讨论】:

    • 谢谢。就是这样!但是,解决方案 itefl(来自 Google)看起来很奇怪,因为服务帐户无法直接访问,但可以假冒(最终用户没有任何用户交互)
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2018-10-06
    • 2013-08-30
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多