我正在用WPF(Windows 10桌面)编写一个应用程序,它应该
包含用户可以下载消息头的组件
以及来自G-Mail的邮件。
我正在尝试使用MailKit通过安全
连接(无需打开“允许不太安全的应用”
正确的程序。
仅供参考:我对OAuth和TLS几乎一无所知,所以请亲吻。
我已经从Google创建并下载了OAuth 2.0的JSON文件。
我访问了MailKit的常见问题解答,以及以下部分
接口。
(请参阅下面的代码。)
对于“密码”,这是该帐户的密码吗?
我不知道该给什么
"your-developer-id@developer.gserviceaccount.com".
.........................................................
https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog
.........................................................
来自问答;答:
如何使用OAuth 2.0登录GMail帐户?
你需要做的第一件事是按照谷歌的指示
获取应用程序的OAuth 2.0凭据。
完成此操作后,获取访问令牌的最简单方法是
使用谷歌的Google.Apis。身份验证库:
var certificate = new X509Certificate2 (@"C:\path\to\certificate.p12", "password",
X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential (new ServiceAccountCredential
.Initializer ("your-developer-id@developer.gserviceaccount.com") {
// Note: other scopes can be found here: [links]
Scopes = new[] { "https mail google com " },
User = "username@gmail.com"
}.FromCertificate (certificate));
bool result = await credential.RequestAccessTokenAsync (CancellationToken.None);
// Note: result will be true if the access token was received successfully
// Now that you have an access token (credential.Token.AccessToken), you can
// use it with MailKit as if it were the password:
using (var client = new ImapClient ()) {
client.Connect ("imap.gmail.com", 993, true);
// use the access token as the password string
client.Authenticate ("username@gmail.com", credential.Token.AccessToken);
}
我的下一个问题:用户是否可以访问自己的帐户
使用我的应用程序而不必遵循相同的程序?
IOW:我下载的凭证是否适用于任何帐户?
…或仅允许访问凭据来自的帐户
是否已创建?
如果凭据只对我自己的帐户有效,那么我就必须
做点别的。
谷歌登录是一种更好的方法吗?
我已经从Google下载了.NET的示例代码:
https://github.com/googlesamples/oauth-apps-for-windows
我构建并运行了“OAuthConsoleApp”和“OAuthDesktopApp”。
看起来我已经从那里获得了安全连接,
因为我得到了以下输出:
.........................................................
重定向URI:http 127.0.0.1:64003
听。。
授权码:qwerty。。。
正在为令牌交换代码。。。
等待流。WriteAsync。。。
获取响应。。。
响应文本。。。
{
“token_type”:“持有者”,
“到期时间_ in”:3600,
“refresh_token”:“qwerty…”,
“id_token”:“qwerty…”
}
+++用户信息响应文本:{
“子”:“117108120545711995673”,
“name”:“我的名字”,
“given_name”:“我的”,
“picture”:“qwerty…”,
“语言环境”:“en”
}
.....................................................
我看到响应中有一个“access_token”,我想
我可以将其插入“client.Authenticate”方法
MailKit作为密码(如MailKit文档中所述):
string access_token = tokenEndpointDecoded["access_token"];
client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);
client.Authenticate ("username@gmail.com", access_token);
它引发了一个异常:
.....................................................
“POP3服务器未对AUTH命令作出+OK响应。”
位于MailKit.Net.Pop3.Pop3Client。身份验证(编码编码,
ICredentials凭据、CancellationToken取消令牌)
位于MailKit.MailService。身份验证(字符串用户名,字符串
密码,取消令牌取消令牌)
在
NS_MailKit_01.Pop3.cls_MailKit_Pop3_01.connect_and_authenticate(对象
p3_client,字符串p_access_token)
in :\Software_Develpoment_Sys_03_K\MIME_EMail\TEST_02\Mail_Kit_01\MailKit_01.cs:line
465
在
p_access_token)
in K:\Software_Develpoment_Sys_03_K\MIME_EMail\TEST_02\OAuth\oauth-apps-for-windows\OAuthConsoleApp\LIB1_01_G_Mail_Auth\G_Mail_Auth_01.cs:
第95行
在
in K:\Software_Develpoment_Sys_03_K\MIME_EMail\TEST_02\OAuth\oauth-apps-for-windows\OAuthConsoleApp\LIB1_01_G_Mail_Auth\G_Mail_Auth_01.cs:line
343
.....................................................
有人知道我如何从
任何帮助都将不胜感激。
谢谢