我正在尝试配置能够与Gmail API一起使用的应用程序。如您所知,必须使用访问令牌。请求令牌的方法有多种,但是对我来说,应该是一个服务帐户,因为将来该程序代码将在Windows Service中...(因此,没有机会通过从Windows重定向手动接收令牌)。 Google URL,只有网络请求和响应才是出路)
所以,我已经做了:
const string serviceAccountEmail = "***test@oauthtester-271011.iam.gserviceaccount.com";
const string serviceAccountCertPath = @"C:\Users\user\Documents\Visual Studio 2017\Projects\OAuthTester\OAuthTester\bin\Debug\oauthtester-271011-bd2cced31ea5.p12";
const string serviceAccountCertPassword = "notasecret";
const string userEmail = "***oauthtest@***.com";
X509Certificate2 certificate = new X509Certificate2(
serviceAccountCertPath,
serviceAccountCertPassword,
X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { GoogleScope.ImapAndSmtp.Name }, //"https://mail.google.com/"
User = userEmail
}.FromCertificate(certificate));
credential.RequestAccessTokenAsync(CancellationToken.None).Wait();
不幸的是,我遇到了一个错误:
Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.
我也尝试过:
不幸的是,每次我遇到相同的错误。也许有人猜到我做错了什么?