我试图使用MailKit (http://jstedfast.github.io/MailKit/docs/index.html)来使用oAuth登录Gmail。我可以使用刷新的AuthToken登录Google,但是当我尝试使用MailKit中的刷新令牌时,我会得到一个错误“无效凭据”。
有什么线索吗?谢谢,杰夫
这是我的代码:
var secrets = new ClientSecrets()
{
ClientId = "xxx-yyy.apps.googleusercontent.com",
ClientSecret = "xyzSecret"
};
IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets,
Scopes = new string[] { GmailService.Scope.GmailReadonly }
});
var tokenResponse = flow.RefreshTokenAsync("", connection.RefreshToken, CancellationToken.None).Result;
using (var client = new MailKit.Net.Imap.ImapClient())
{
var credentials = new NetworkCredential("emailtoconnect@gmail.com", tokenResponse.AccessToken);
client.Connect("imap.gmail.com", 993, true, CancellationToken.None);
try
{
client.Authenticate(credentials, CancellationToken.None);
}
catch (Exception ex)
{
throw;
}
}发布于 2017-07-16 06:50:06
一个更清楚的答案是,原代码中的作用域不正确。
Scopes = new string[] { GmailService.Scope.GmailReadonly }需要的是
Scopes = new string[] { GmailService.Scope.MailGoogleCom }以便使用访问令牌对imapi进行身份验证。
发布于 2014-12-02 14:27:17
只是个范围问题。以上代码适用于gmail oAuth!
发布于 2016-06-30 09:35:12
using (var client = new ImapClient())
{
client.Connect("imap.gmail.com", 993, true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(EmailId, Password);
}上面的代码用于使用imap和MailKit工具登录到gmail。但在此之前,您必须手动登录到gmail,并在Settings中选中"Enable“选项。这肯定行得通。
https://stackoverflow.com/questions/27236947
复制相似问题