Google.Apis.Requests.RequestError - 需要 OAuth 2 访问令牌

发布于 2025-01-18 04:50:07 字数 2841 浏览 1 评论 0原文

执行 Google.Apis.Gmail.v1.StopRequest.Stop 时,我们在 C# 中收到以下错误。 **注意:此代码已运行 2 年多,并在 2022 年 3 月 23 日左右突然停止工作。我还提供了我们正在使用的 Nuget 版本的屏幕截图。对此有任何建议或指导,我们将不胜感激!

Google. Apis Nugets

代码:

 var credential = GetCredential(serviceAccountEmail, certFileName, email);

            if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
            {
                var accessToken = credential.Token.AccessToken;
                GmailService gmailService = new GmailService(
                    new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential

                    }
                );

                var stopRequest = gmailService.Users.Stop(email);

                stopRequestStatus = stopRequest.Execute();

            }

这是 GetCredentials 的代码:

ServiceAccountCredential GetCredential(string serviceAccountEmail, string certFileName, string userId)
    {
        try
        {
            log.Debug("svcAacntEmail=" + serviceAccountEmail);
            log.Debug("Userid=" + userId);
            log.Debug("certfilename" + certFileName);

            X509Certificate2 certificate = new X509Certificate2(certFileName, SvcResource.CERT_PASSWORD, X509KeyStorageFlags.Exportable);


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


            return credential;
        }
        catch (Exception ex)
        {
            log.Error("Error in setting creds for gmail svc auth", ex);
            return null;
        }
    }

错误:

gmail 服务抛出异常:

Google.GoogleApiException:Google.Apis.Requests.RequestError 请求的身份验证凭据无效。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。请参阅 https://developers.google.com/identity/sign-in /web/devconsole-project。 【401】 错误[ 消息[无效凭证] 位置[授权 - 标头] 原因[authError] 域[全局] ]

at Google.Apis.Requests.ClientServiceRequest1.d__31.MoveNext() --- 抛出异常的上一个位置的堆栈跟踪结束 --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo Google.Apis.Requests.ClientServiceRequest 处的 .Throw()1.Execute()

We are getting the following error in c# when executing Google.Apis.Gmail.v1.StopRequest.Stop. **NOTE: this is code that has ran for 2+ years and suddenly stopped working around 3/23/2022. I am also including a screenshot of the Nuget versions we are using. Any advice or direction on this is appreciated!

Google.Apis Nugets

THE CODE:

 var credential = GetCredential(serviceAccountEmail, certFileName, email);

            if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
            {
                var accessToken = credential.Token.AccessToken;
                GmailService gmailService = new GmailService(
                    new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential

                    }
                );

                var stopRequest = gmailService.Users.Stop(email);

                stopRequestStatus = stopRequest.Execute();

            }

This is the code for GetCredentials:

ServiceAccountCredential GetCredential(string serviceAccountEmail, string certFileName, string userId)
    {
        try
        {
            log.Debug("svcAacntEmail=" + serviceAccountEmail);
            log.Debug("Userid=" + userId);
            log.Debug("certfilename" + certFileName);

            X509Certificate2 certificate = new X509Certificate2(certFileName, SvcResource.CERT_PASSWORD, X509KeyStorageFlags.Exportable);


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


            return credential;
        }
        catch (Exception ex)
        {
            log.Error("Error in setting creds for gmail svc auth", ex);
            return null;
        }
    }

THE ERROR:

The service gmail has thrown an exception:

Google.GoogleApiException: Google.Apis.Requests.RequestError
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
Errors [
Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]
]

at Google.Apis.Requests.ClientServiceRequest1.<ParseResponse>d__31.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Google.Apis.Requests.ClientServiceRequest1.Execute()

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

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

发布评论

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

评论(1

忆依然 2025-01-25 04:50:07

我在 C# 应用程序中也遇到了这个问题

我现在已经解决了问题 - 这与 401 错误消息所暗示的内容无关。

我们的应用程序是用 C# 编写的,默认情况下尝试使用 TLS 1.0。当我们强制它使用 TLS 1.2 时,它工作得绝对正常。

所以真正的问题是 HTTPS 连接中使用的加密密码,与授权标头等无关。

I ran into this issue as well in our C# application.

I've now figured out the problem - and it's nothing to do with what the 401 error message suggests.

Our application is written in C# and is attempting to use TLS 1.0 by default. When we force it to use TLS 1.2, it works absolutely fine.

So the real problem is the cryptography ciphers used in the HTTPS connection and is nothing to do with the Authorization header, etc.

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