gpt4 book ai didi

google-cloud-platform - 使用服务帐户访问用户帐户

转载 作者:行者123 更新时间:2023-12-04 14:16:19 24 4
gpt4 key购买 nike

我们需要使用 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

Only users with access to the Admin APIs can access the Admin SDK Directory API, therefore your service account needs to impersonate one of those users to access the Admin SDK Directory API. Additionally, the user must have logged in at least once and accepted the Google Workspace Terms of Service.


但它只是没有点击我应该如何做到这一点 - 这是隐藏在其中一个管理控制台中的一些设置吗?不 - 您将其作为初始连接的一部分传递。
所以只是为了将说明放在一个地方,并希望避免其他人同样的头痛,这些是步骤:
  • 通过 https://console.developers.google.com 创建了一个项目
  • 搜索然后启用 Admin SDK API

  • 创建了一个服务帐户
  • 显示域范围委派/启用 G Suite 域范围委派
  • 此时我有一个服务帐户名称、唯一 ID、电子邮件和客户端 ID
  • 它生成了我下载的 key 文件 (json)。

  • 转至:https://admin.google.com
  • 安全 > API 控制。
  • 管理域范围委派
  • 使用上面的客户端 ID 添加了条目,应用了这两个范围 - 您可以根据需要应用其他范围。
  • https://www.googleapis.com/auth/admin.directory.user.readonly
  • https://www.googleapis.com/auth/admin.directory.group.member.readonly




  • 然后我创建了一个 .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;
    }
    }
    希望这可以为其他人省去一些麻烦。

    关于google-cloud-platform - 使用服务帐户访问用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59934747/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com