【问题标题】:Requested client not authorized请求的客户未授权
【发布时间】:2014-05-27 11:30:38
【问题描述】:

我正在尝试使用 google service account 从我的域中获取 google 用户。

但是会报错

Error:"access_denied", Description:"Requested client not authorized.", Uri:""

我的代码

X509Certificate2 certificate = new X509Certificate2(key_path,
                         "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer("publickey.gserviceaccount.com")
           {   Scopes = scopes,
               User = "admin@domain.com"
           }.FromCertificate(certificate));

var service = new DirectoryService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "appname",
        });

service.Users.List().Domain = "domain.com";
Users results = service.Users.List().Execute();

提前致谢

【问题讨论】:

  • 你已经尝试过什么?很多事情都可能出错,证书、证书的安装、您使用的用户等等......

标签: c# asp.net google-app-engine google-api google-apps


【解决方案1】:

服务帐户电子邮件地址需要访问域。获取电子邮件并将其添加为用户,它可以阅读的足够访问权限应该是好的。

您是否也更改了此内容以进行发布?

"publickey.gserviceaccount.com"

服务帐户电子邮件看起来更像这样:

539621478854-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com

【讨论】:

  • 您是否授予服务帐户读取您域中的 Google 用户的权限?
  • 让我们再试一次。您是否转到您的域并将服务帐户添加为允许其读取此信息的用户?
  • 我已在我的开发者控制台中设置了此服务帐户的编辑权限。我仍然收到拒绝访问错误。任何帮助将不胜感激。
【解决方案2】:

您需要首先授予您的服务帐户/API 项目对您的域的访问权限。 此处的文档中详细说明了步骤:

https://developers.google.com/admin-sdk/directory/v1/guides/delegation#delegate_domain-wide_authority_to_your_service_account

您需要在这些说明的第 6 步中指定所需的正确范围,即 https://www.googleapis.com/auth/admin.directory.user.readonly 以访问用户列表。

为了使 Directory API 正常工作,您需要在域设置中启用 API 访问:https://developers.google.com/admin-sdk/directory/v1/guides/prerequisites#set_up_api

【讨论】:

  • 以上你提到的几点都已经完成了。我仍然收到拒绝访问错误。这个问题可能是由于我正在使用的 DLL - 我从 Google Google.Apis.Admin.Directory.directory_v1.Data 下载的。是否有此 DLL 的更高版本?
  • 所有最新的客户端库文件都在这里,几天前有一个新的更新,但我认为这不应该是问题:nuget.org/…
  • 只是为了确保设置步骤正确:在管理控制台中设置期间,您必须使用看起来像 randomstring.apps.googleusercontent.comClient ID,对于 ServiceAccountCredential,您必须使用 Email address 这看起来像randomstring@developer.gserviceaccount.com
  • 是的,我已经再次验证了这些步骤,但我仍然收到此错误。
  • 您好,我已经分配了这样的证书文件路径 --string path = "d:\\5e0938f59e0d3b88508670ce1c6b82065f26fbb0-privatekey.p12";-- 这是否正确?请验证。
【解决方案3】:

我终于能够得到这个工作。这是我的代码

        var grpReq = service.Groups.List();
        grpReq.Domain = "mydomain.com";
        Groups groups = grpReq.Execute();

        IList<Group> gps = groups.GroupsValue;

        var memReq=service.Members.List(groups.GroupsValue[0].Id);
        Members members = memReq.Execute();

我仍然不确定为什么创建一个 var 对象然后 Execute() 让它工作,但早期的代码没有工作。

我仍然遇到向所有用户显示同意屏幕的问题。我有以下代码。我认为我获取登录用户电子邮件的方式不正确。有什么想法吗?

        string mymail = googleauth.GetUsersEmail(ExchangeCodeWithAccessAndRefreshToken().Access_Token);

        string path = "d:\\c6b82065f26fbb0-privatekey.p12";
        X509Certificate2 certificate = new X509Certificate2(
            path,
            "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
          new ServiceAccountCredential.Initializer("876131792-v824u6drpss@developer.gserviceaccount.com")
          {
              User = mymail,
              Scopes = new[] { PlusService.Scope.UserinfoEmail, PlusService.Scope.UserinfoProfile, PlusService.Scope.PlusMe }
          }.FromCertificate(certificate));


        PlusService plus = new PlusService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "myapp"
        });

        Person profile = plus.People.Get("me").Execute();
        string email = profile.Emails[0].Value;

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 2015-02-01
    • 2014-10-08
    • 2014-11-06
    • 1970-01-01
    • 2015-07-06
    • 2023-03-30
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多