2016-08-30 72 views
0

我正在WPF(Windows 10桌面)上編寫一個應用程序,該應用程序應該包含一個組件,用戶可以在其中下載消息標題 和來自G-Mail的消息。Google登錄和MailKit之間的界面

我試圖使用MailKit通過安全連接 與G-郵件接口(而無需打開「允許安全性較低的應用程序」 爲G-郵件),並與POP3下載郵件。至於正確的程序,我有點困惑 。

僅供參考:我幾乎不知道OAuth和TLS,所以KISS請。

我已經爲Google的OAuth 2.0創建並下載了一個JSON文件。

我已經訪問了MailKit的常見問題,下面的部分 似乎相關,但我不確定我應該在接口中插入什麼 。

(請參閱下面的代碼。)

對於「密碼」,那又是該帳戶的密碼?我不確定 「[email protected]」的內容。

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

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

從Q &答:

我怎樣才能登錄到Gmail帳戶使用OAuth 2.0?

您需要做的第一件事是按照Google的 指示獲取適用於您的應用程序的OAuth 2.0憑據。

一旦你做到了這一點,以獲得訪問令牌的最簡單方法是使用 谷歌的Google.Apis.Auth庫:

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 Sign-In會是一個更好的方法嗎?

我已經下載了示例代碼。NET from Google: https://github.com/googlesamples/oauth-apps-for-windows

我已經構建並運行了「OAuthConsoleApp」以及「OAuthDesktopApp」。

這似乎是我收到那些安全連接, 因爲我已經得到了以下的輸出:

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

重定向URI:http 127.0.0.1 :64003

聽力..

授權代碼:QWERTY鍵盤...

換取令牌代碼...

發送的請求......

GetRequestStream ...

等待stream.WriteAsync ...

得到的迴應...

的responseText ...

{

「的access_token」: 「QWERTY ......」,

「token_type」: 「承載」

「expires_in」:3600,

「refresh_token」: 「QWERTY ......」

「id_token」: 「QWERTY ......」

}

使API調用用戶信息...

+++ userinfoResponseText:{

「子」: 「117108120545711995673」,

「名」: 「我的名字」,

「GIVEN_NAME」: 「我的」,

「FAMILY_NAME」: 「姓名」,

「圖片報」: 「QWERTY ......」,

「區域設置」: 「恩」

}

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

我看到我在響應中有一個「access_token」,我想 我可以將它插入到「客戶端」。認證」方法 MailKit作爲口令(如在該文檔中提到用於MailKit):

string access_token = tokenEndpointDecoded["access_token"]; 

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

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

它引發了異常:

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

「POP3服務器沒有響應對AUTH命令有+ OK響應。「

at MailKit.Net.Pop3.Pop3Client.Authenticate(編碼編碼, 個ICredentials憑證,的CancellationToken的CancellationToken)

在MailKit.MailService.Authenticate(用戶名字符串,字符串 密碼,的CancellationToken的CancellationToken)

在 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 

在 LIB1_01_G_Mail_Auth.cls_G_mail_authorization.str_token_NTRF.invoke_access_token(字符串 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

在 LIB1_01_G_Mail_Auth.cls_G_mail_authorization.d__13.MoveNext()

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 

。 .................................................. ..

有誰知道我可以如何從 獲得一個可用於MailKit的Google界面的「憑證」對象?

任何幫助,將不勝感激。

謝謝!

回答

0

對於「密碼」,這是該帳戶的密碼?

不會。它是包含您的X.509證書和您的私鑰的PKCS12文件的密碼。

我不確定應該給「[email protected]」什麼。

您需要使用Google的開發人員程序註冊您自己和您的應用程序,該程序會爲您提供一個開發人員ID以供使用。你需要遵循他們的指示。