【问题标题】:Service account delegation with Google workspace - Entity Not found使用 Google 工作区委派服务帐户 - 找不到实体
【发布时间】:2021-12-11 15:27:03
【问题描述】:

我正在尝试访问 Google Directory api 作为测试此方法domains.list 使用服务帐户。

如果我使用我的域管理员电子邮件登录该页面上的 try me。它有效,我得到了回复。所以我传递的方法和客户 ID 应该可以工作。

我按照Perform Google Workspace Domain-Wide Delegation of Authority 此处的说明创建了一个服务帐户并启用了域范围的委派。

如果我同时检查我的工作区帐户和 Google 云控制台。委派似乎已配置。以我的域管理员电子邮件为原则。

我的代码:

namespace Daimto.Sample.WorkspaceAdmin
{
    class Program
    {
        private static readonly string[] Scopes = {DirectoryService.Scope.AdminDirectoryDomain};
        private static readonly string PathToServiceAccountKeyFile = @"C:\YouTube\workspaceserviceaccount-e4823a933ae3.json";
        private static readonly string CustomerId = "C01lp3chxa";
        private static readonly string workspaceAdmin = "xxx@daimto.com";

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var credential = LoadGoogleCredentails();
            var service = CreateDirectoryService(credential);
            var request = service.Domains.List(CustomerId);
            var result = request.Execute();
            foreach (var domain in result.Domains)
            {
                Console.WriteLine(domain.DomainName);
            }
        }

        private static DirectoryService CreateDirectoryService(GoogleCredential credential)
        {
            return new (new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Daimto Testing Workspace with service account"
                }
            );
        }

        private static GoogleCredential LoadGoogleCredentails()
        {
            return GoogleCredential.FromFile(PathToServiceAccountKeyFile)
                .Impersonate(new ImpersonatedCredential.Initializer(workspaceAdmin))
                .CreateScoped(Scopes);
        }
    }
}

错误

如上所示运行代码。

 "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "errors": [
      {
        "message": "Requested entity was not found.",
        "domain": "global",
        "reason": "notFound"
      }

未找到对我来说意味着它甚至无法访问该域。

但是,如果我删除模拟行。然后我得到这个错误

Not Authorized to access this resource/api [403]
Errors [
  Message[Not Authorized to access this resource/api] Location[ - ] Reason[forbidden] Domain[global]
]

这对我来说意味着如果没有模拟它就没有访问权限。

所以我很困惑。使用模拟找不到它,没有模拟它可以找到它但没有访问权限?

我在文档中能找到的唯一线索就是这个开始的注释。

那么,Only users with access to the Admin APIs can access the Admin SDK Directory API 我是管理员,我不应该有访问权限吗?如果需要,应该在哪里配置访问权限?

【问题讨论】:

    标签: c# google-admin-sdk google-workspace google-api-dotnet-client service-accounts


    【解决方案1】:

    好的,这需要大量的挖掘。

    Google .Net 客户端库中有两种方法Impersonate()CreateWithUser()

    Impersonate 允许此凭据模拟 ImpersonatedCredential.Initializer.TargetPrincipal。只有 ServiceAccountCredential 和 UserCredential 支持模拟,因此此方法将抛出

    同时

    CreateWithUser 如果凭据支持域范围委派,则此方法会创建具有指定用户的凭据副本。否则,它会抛出

    所以这里的关键是您尝试委派给用户还是冒充用户。有区别。

    所以现在使用 CreateWithUser 可以正常工作了。

     private static GoogleCredential LoadGoogleCredentails()
            {
                return GoogleCredential.FromFile(PathToServiceAccountKeyFile)
                    .CreateScoped(Scopes)
                    .CreateWithUser(workspaceAdmin);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多