关于wpf:Google登录和MailKit之间的接口

Interface Between Google Sign-in and MailKit

我正在WPF(Windows 10桌面)中编写一个应
包括一个组件,用户可以在其中下载消息头
和来自G-Mail的消息。

我正在尝试使用MailKit通过安全接口与G-Mail交互
连接(无需打开"允许安全性较低的应用"
(用于G-Mail),并使用POP3下载邮件。我有一点疑惑
关于正确的程序。

仅供参考:我对OAuth和TLS几乎一无所知,所以请KISS。

我已经从Google创建并下载了OAuth 2.0的JSON文件。

我已经访问了MailKit的常见问题解答,以及以下部分
似乎相关,但我不确定应该插入什么
到接口。

(请参见下面的代码。)

对于"密码",那是该帐户的密码吗?

我不确定该给什么
" [email protected]"。

.........................................................

https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog
.........................................................

From the Q & A:

How can I log in to a GMail account using OAuth 2.0?

The first thing you need to do is follow Google's instructions for
obtaining OAuth 2.0 credentials for your application.

Once you've done that, the easiest way to obtain an access token is to
use Google's Google.Apis.Auth library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var certificate = new X509Certificate2 (@"C:\\path\\to\\certificate.p12","password",
         X509KeyStorageFlags.Exportable);

var credential = new ServiceAccountCredential (new ServiceAccountCredential
    .Initializer ("[email protected]") {

    // Note: other scopes can be found here: [links]

    Scopes = new[] {"https  mail google com" },

    User ="[email protected]"

}.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 ("[email protected]", credential.Token.AccessToken);
}

我的下一个问题:用户是否可以访问自己的帐户
使用我的应用程序而不必遵循相同的步骤?

IOW:我下载的凭据对任何帐户都有效吗?
...或仅允许访问其凭据来源的帐户
被创建了吗?

如果凭据仅对我自己的帐户有用,那么我将不得不
做其他事情。

Google登录会是更好的方法吗?

我已经从Google下载了.NET的示例代码:
https://github.com/googlesamples/oauth-apps-for-windows

我已经构建并运行了" OAuthConsoleApp"以及" OAuthDesktopApp"。

似乎我从这些人那里得到了安全的联系,
因为我得到以下输出:

.........................................................

redirect URI: http 127.0.0.1:64003

Listening..

Authorization code: qwerty ...

Exchanging code for tokens...

Send the request ...

GetRequestStream ...

await stream.WriteAsync ...

Get the response ...

responseText ...

{

"access_token":"qwerty ...",

"token_type":"Bearer",

"expires_in": 3600,

"refresh_token":"qwerty ...",

"id_token":"qwerty ..."

}

Making API Call to Userinfo...

+++ userinfoResponseText : {

"sub":"117108120545711995673",

"name":"My Name",

"given_name":"My",

"family_name":"Name",

"picture":"qwerty ...",

"locale":"en"

}

.....................................................

我看到我的回复中有一个" access_token",我想
我可以将其插入" client.Authenticate"方法中
MailKit作为密码(如MailKit文档中所述):

1
2
3
4
5
string access_token = tokenEndpointDecoded["access_token"];

client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);

client.Authenticate ("[email protected]", access_token);

引发异常:

.....................................................

"POP3 server did not respond with a +OK response to the AUTH command."

at MailKit.Net.Pop3.Pop3Client.Authenticate(Encoding encoding,
ICredentials credentials, CancellationToken cancellationToken)

at MailKit.MailService.Authenticate(String userName, String
password, CancellationToken cancellationToken)

at
NS_MailKit_01.Pop3.cls_mailKit_Pop3_01.connect_and_authenticate(Object
p3_client, String p_access_token)

1
in :\\Software_Develpoment_Sys_03_K\\MIME_EMail\\TEST_02\\Mail_Kit_01\\MailKit_01.cs:line

465

at
LIB1_01_G_Mail_Auth.cls_G_mail_authorization.str_token_NTRF.invoke_access_token(String
p_access_token)

1
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 95

at
LIB1_01_G_Mail_Auth.cls_G_mail_authorization.d__13.MoveNext()

1
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

.....................................................

有人知道我如何从中获取"凭据"对象
我可以与MailKit一起使用的Google界面?

我们将不胜感激。

谢谢!


For"password", would that be the password for the account?

不。这将是包含X.509证书和私钥的PKCS12文件的密码。

I'm not sure as to what to give for"[email protected]".

您需要使用Google的Developer程序注册自己和您的应用程序,这将为您提供要使用的Developer ID。您需要遵循他们的指示。