Gmail API突然停止工作。错误消息说未授权的_client

发布于 2025-01-29 05:06:26 字数 163 浏览 4 评论 0原文

自过去4年以来,我们一直在使用Gmail API来阅读电子邮件,但是它突然停止工作并投掷例外:错误:“ unuthorized_client”,Description:“ client:“未经授权无法使用此方法检索访问令牌,或 范围。

客户未

授权任何要求的

We have been using Gmail API since the last 4 years without issue to read emails, but it suddenly stopped working and throwing exceptions: Error: "unauthorized_client", Description: "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.", Uri:"

Checked the service account and it's active, so no issue there as well.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2025-02-05 05:06:26

错误:“ unuthorized_client”,description:“未经授权使用此方法检索访问令牌,或者客户未授权任何要求的范围。”,uri:“

。不确定的是,

使用.NET使用的Google OUATH的三种类型的

凭据

可以 您会说,您使用的是使用它的代码,

如果您使用的是服务帐户,请确保

  1. 您使用的代码是用于服务帐户的。
  2. 因此, 帐户
  3. Workspace

Google

        string ApplicationName = "Gmail API .NET Quickstart";
        const string serviceAccount = "[email protected]";

        var certificate = new X509Certificate2(@"D:\xxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);

        var gsuiteUser = "[email protected]";

        var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
        {
            User = gsuiteUser,
            Scopes = new[] { GmailService.Scope.GmailSend, GmailService.Scope.GmailLabels }

        }.FromCertificate(certificate);

        var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
        if (!credential.RequestAccessTokenAsync(CancellationToken.None).Result)
            throw new InvalidOperationException("Access token failed.");

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

​-net/“ rel =” nofollow noreferrer”>带有Google Workspace和.net的Gmail API

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

Is a fairly common error, why you are first getting it now im not sure.

There are three different types of credentials for google Ouath that can be used with .net. Desktop app, installed app and service account.

The credentials are different for each one. The code that uses them is also different.

The error message you are getting says that you are using credentials with code that it is not designed for.

So if you are using a service account, make sure that

  1. the code you are using is for a service account.
  2. That you have domain wide delegation configured with your google workspace account.
  3. Make sure nothing was changed in the workspace configuration.

service account

Your code should look something like this.

        string ApplicationName = "Gmail API .NET Quickstart";
        const string serviceAccount = "[email protected]";

        var certificate = new X509Certificate2(@"D:\xxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);

        var gsuiteUser = "[email protected]";

        var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
        {
            User = gsuiteUser,
            Scopes = new[] { GmailService.Scope.GmailSend, GmailService.Scope.GmailLabels }

        }.FromCertificate(certificate);

        var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
        if (!credential.RequestAccessTokenAsync(CancellationToken.None).Result)
            throw new InvalidOperationException("Access token failed.");

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

Gmail api with google workspace and .net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文