【问题标题】:Client is unauthorized to retrieve access tokens using this method Gmail API C#客户端未经授权使用此方法检索访问令牌 Gmail API C#
【发布时间】:2017-08-04 17:01:12
【问题描述】:

当我尝试使用服务帐户授权 gmail api 时出现以下错误

“客户端未经授权无法使用此方法检索访问令牌”

static async Task MainAsync()
    {

        sstageEntities db = new sstageEntities();
        //UserCredential credential;
        Dictionary<string, string> dictionary = new Dictionary<string, string>();    
String serviceAccountEmail =
"xxx.iam.gserviceaccount.com";

        var certificate = new X509Certificate2(
            AppDomain.CurrentDomain.BaseDirectory +
              "xxx-8c7a4169631a.p12",
            "notasecret",
            X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

        //string userEmail = "user@domainhere.com.au";

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                User = "xxx@xxx.com",
                Scopes = new[] { "https://mail.google.com/" }
            }.FromCertificate(certificate)
        );


        // Create Gmail API service.
        var gmailService = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.

        var emailListRequest = gmailService.Users.Messages.List("xxx@xxx.com");
        emailListRequest.LabelIds = "INBOX";
        emailListRequest.IncludeSpamTrash = true;
        emailListRequest.Q = "from:bpm@xxx.co.in is:unread";



        //Get our emails
        var emailListResponse = await emailListRequest.ExecuteAsync();

我正在使用我在创建服务帐户时获得的 p12 密钥。但是当我运行我的控制台应用程序时出现以下错误。任何帮助将不胜感激。

提前致谢!

【问题讨论】:

  • 您是否将服务帐户添加到 gsuite? developers.google.com/identity/protocols/…
  • 那么只有组织的管理员才能做到这一点?我只是一个员工?
  • 是的,我认为这是只有管理员才能为您做的事情。
  • Hey DamlmTo 是的,管理员现在必须授予访问权限,这一切都解决了,非常感谢

标签: c# gmail-api


【解决方案1】:

服务帐号需要授权,否则无法访问该域的电子邮件。

“客户端未经授权无法使用此方法检索访问令牌”

表示你没有正确授权检查Delegating domain-wide authority to the service account

【讨论】:

  • 服务帐户仅适用于 GSuite,因为您必须能够预授权服务帐户并授予其访问用户帐户的权限。无法对普通用户的 Gmail 帐户进行预授权。因此,不,您不能将服务帐户与普通用户的 gmail 帐户一起使用。
  • 感谢 DalmTo 的澄清。
  • @DaImTo,我发现有时“将域范围的权限委托给服务帐户”需要更长的时间才能生效。通常是几秒钟,但也可能超过 5 分钟。为什么会这样?对这个问题有什么建议吗?谢谢。
  • 重要说明:在 G Suite Admin 的“管理 API 客户端访问”页面上添加服务帐户及其范围之前,必须启用“将域范围的权限委派给服务帐户”。否则会失败,需要重新添加。
  • @boydenhartog 你不应该是不允许的。普通 gmail 帐户不支持服务帐户。您需要使用 oauth2 并验证您的帐户一次,然后使用刷新令牌再次访问它。
【解决方案2】:

FWIW,由于我对评论太陌生,DalmTo 和 Shane 的回答为我的问题指明了正确的方向,即我添加到现有脚本 (PHP) 中的新功能需要额外范围的授权服务帐户。就我而言,我正在使用 GMail API。

除了 Shane 引用的 Google 文档页面中提到的路径之外,您还可以转到 https://admin.google.com/ac/owl/domainwidedelegation,在那里您可以在稍微不同的界面中管理域范围的委派(我实际上更喜欢它)。我通过“安全”>“API 权限”进入该页面,然后单击有关这些设置移至“应用程序访问控制”的通知,底部有一个“管理域范围委派”链接。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,我花了一段时间才意识到我错过了什么。我已经在服务帐户设置中设置了域范围的权限。但是,根据下面的链接和屏幕截图,我没有在域安全设置中添加 clientid 和 API 范围(例如 https://www.googleapis.com/auth/gmail.sendhttps://mail.google.com/)。

    https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority

    具体来说,这些步骤:

    【讨论】:

      【解决方案4】:

      在我的情况下,授权已授予 https://admin.google.com/ 中的旧客户端 ID

      在为范围提供正确的客户端 ID 后,它开始工作。 (使用上述答案中DalmTo 提到的指南链接。

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 1970-01-01
        • 2019-01-27
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-19
        相关资源
        最近更新 更多