我正在尝试使用 OAUTH 2.0 身份验证发送一封电子邮件,如下所示:
var certificate = new X509Certificate2(@"C:\path\to\my.p12", "mypassword", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential
.Initializer("my@id.gserviceaccount.com")
{
Scopes = new[] { "https://mail.google.com/" },
User = "scm.beartung@gmail.com"
}.FromCertificate(certificate));
bool result = await credential.RequestAccessTokenAsync(CancellationToken.None);
// send email
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("user.name", credential.Token.AccessToken);
client.Send(myMessage);
client.Disconnect(true);
}
但是,当我运行此代码时,我得到了 TokenResponseException:“客户端未经授权无法使用此方法检索访问令牌。” 我需要授权我的客户是否正确,只有 G Suit 帐户才有可能?所以我必须为管理员帐户付费或删除 OAUTH?