【问题标题】:Google Admin SDK - Exception access_denied Requested client not authorizedGoogle Admin SDK - 异常 access_denied 请求的客户端未授权
【发布时间】:2014-10-08 10:45:55
【问题描述】:

我希望有人可以帮助我。

请看下面的源代码。当 Execute 最后被击中时。我会收到以下错误:

Google.Apis.Auth.OAuth2.Responses.TokenResponseException 未处理 _HResult=-2146233088 _message=Error:"access_denied", Description:"请求的客户端未授权。", Uri:""

我已经尝试了很多东西。创建用户、列表组等(使用正确的范围),但每次我都遇到同样的错误。

我创建了一个服务帐户,并且 Google Developers Console 中的所有 API 都设置为“开启” 我用于模拟的用户是完全管理员。

我已经尝试了几件事,但我必须做一些愚蠢的事情。 该代码看起来很像 Stack Overflow 上的另一个问题:Google Admin SDK Unable to Create User - Exception 403 Forbidden 但我的行不通。谁能告诉我我忘记了什么?

private static readonly string[] Scopes = new string[] { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryGroup, DirectoryService.Scope.AdminDirectoryGroupMember };

    static void Main(string[] args)
    {

        String serviceAccountEmail = "***@developer.gserviceaccount.com";
        var certificate = new X509Certificate2(@"file.p12", "notasecret", X509KeyStorageFlags.Exportable);
        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               Scopes = Scopes,
               User = "admin email address",

           }.FromCertificate(certificate));
        var service = new DirectoryService(new BaseClientService.Initializer()
       {
           HttpClientInitializer = credential,
           ApplicationName = "User Provisioning",
       });

        User newuserbody = new User();
        UserName newusername = new UserName();
        newuserbody.PrimaryEmail = "bbacon@correct.domain";
        newusername.GivenName = "Bob";
        newusername.FamilyName = "Bacon";
        newuserbody.Name = newusername;
        newuserbody.Password = "iambacon";

        Google.Apis.Admin.Directory.directory_v1.Data.User results = service.Users.Insert(newuserbody).Execute();

        Console.WriteLine("Press any key to continue!");
        Console.ReadKey();
    }

【问题讨论】:

    标签: google-admin-sdk google-api-dotnet-client


    【解决方案1】:

    在您的管理控制台中,您是否专门授予客户端 ID 权限?仅在开发者控制台中打开 API 是不够的,您必须在管理控制台中专门授予此客户端 ID 范围访问权限,以便它模拟您域中的用户。

    1. 转到您的 Google Apps 域的管理控制台。

    2. 从控件列表中选择安全。如果您没有看到列出的安全性,请从页面底部的灰色栏中选择更多控件,然后从控件列表中选择安全性。

    3. 从选项列表中选择高级设置。

    4. 在身份验证部分选择管理第三方 OAuth 客户端访问。

    5. 在“客户名称”字段中输入服务帐户的客户 ID。

    6. 在一个或多个 API 范围字段中,输入应授予您的应用程序访问权限的范围列表。例如,如果您需要在域范围内访问 Google Drive API 和 Google Calendar API,请输入:https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/calendar

    7. 点击授权按钮。

    如果这解决了权限错误,请告诉我。

    【讨论】:

    • 哦...我永远找不到它。过去我确实使用过 Google.Gdata 库。我不记得曾经设置过这个设置。你太棒了!
    猜你喜欢
    • 2014-11-06
    • 2017-02-22
    • 2018-05-27
    • 2015-07-06
    • 2013-10-07
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多