\n
var credential = GetCredential(serviceAccountEmail, certFileName, email);\n\n if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)\n {\n var accessToken = credential.Token.AccessToken;\n GmailService gmailService = new GmailService(\n new BaseClientService.Initializer()\n {\n HttpClientInitializer = credential\n\n }\n );\n\n var stopRequest = gmailService.Users.Stop(email);\n\n stopRequestStatus = stopRequest.Execute();\n\n }\n
\nThis is the code for GetCredentials:
\nServiceAccountCredential GetCredential(string serviceAccountEmail, string certFileName, string userId)\n {\n try\n {\n log.Debug("svcAacntEmail=" + serviceAccountEmail);\n log.Debug("Userid=" + userId);\n log.Debug("certfilename" + certFileName);\n\n X509Certificate2 certificate = new X509Certificate2(certFileName, SvcResource.CERT_PASSWORD, X509KeyStorageFlags.Exportable);\n\n\n ServiceAccountCredential credential = new ServiceAccountCredential(\n new ServiceAccountCredential.Initializer(serviceAccountEmail)\n {\n User = userId,\n Scopes = new[] \n { \n "https://mail.google.com/",\n \n }\n }.FromCertificate(certificate));\n\n\n return credential;\n }\n catch (Exception ex)\n {\n log.Error("Error in setting creds for gmail svc auth", ex);\n return null;\n }\n }\n
\nThe service gmail has thrown an exception:
\n\n\nGoogle.GoogleApiException: Google.Apis.Requests.RequestError\nRequest 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]\nErrors [\nMessage[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]\n]
\n
\n\n","author":{"@type":"Person","name":"Andy"},"upvoteCount":2,"answerCount":1,"acceptedAnswer":null}}at Google.Apis.Requests.ClientServiceRequest
\n1.<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.ClientServiceRequest
1.Execute()
Reputation: 43
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!
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 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.ClientServiceRequest
1.<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.ClientServiceRequest
1.Execute()
Upvotes: 2
Views: 1788
Reputation: 531
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.
Upvotes: 2