客户端无权使用此方法检索访问令牌 Gmail API C#

问题描述 投票:0回答:5

当我尝试使用服务帐户授权 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 = "[email protected]";

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                User = "[email protected]",
                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("[email protected]");
        emailListRequest.LabelIds = "INBOX";
        emailListRequest.IncludeSpamTrash = true;
        emailListRequest.Q = "from:[email protected] is:unread";



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

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

提前致谢!

c# google-api gmail gmail-api google-api-dotnet-client
5个回答
45
投票

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

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

表示您没有正确授权检查将域范围的权限委派给服务帐户


3
投票

FWIW,由于我太新,无法发表评论,DalmTo 和 Shane 的答案为我的问题指明了正确的方向,即我添加到现有脚本(PHP)的新功能需要对服务的其他范围进行授权帐户。就我而言,我正在使用 GMail API。

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


2
投票

在我的例子中,授权已授予 https://admin.google.com/

中的旧客户端 ID

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


2
投票

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

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

具体来说,步骤如下:


0
投票

如果它对任何人有帮助,我发现我必须在 Google Workspace 网域范围授权页面上启用

https://mail.google.com
范围。

尽管事实上我的应用程序只需要这两个范围:

  • https://www.googleapis.com/auth/gmail.send
  • https://www.googleapis.com/auth/gmail.compose

首先,我仅将这两个(发送和撰写)添加到 Google Cloud > API 和服务 > OAuth 同意屏幕 > 编辑应用程序 > 范围页面 (https://console.cloud.google.com/apis/credentials/consent /edit?project=project-name)并访问 Google Workspace 全网域委托页面 (https://admin.google.com/ac/owl/domainwidedelegation)

但是我仍然会收到错误消息:

unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

唯一的解决方案是在 Google Workspace 网域范围授权页面上启用

https://mail.google.com
范围。这是所涉及的最远范围,但我能够在实际 API 配置中仅保留发送和撰写范围。

© www.soinside.com 2019 - 2024. All rights reserved.