【问题标题】:IMAP OAuth2 with Chilkat带有 Chilkat 的 IMAP OAuth2
【发布时间】:2014-09-23 21:43:47
【问题描述】:

我正在寻找一种使用谷歌服务帐户验证 IMAP 会话的方法

但是由于我们已经使用 Chilkat,我们该怎么做,我发现了以下内容:

http://www.cknotes.com/imap-authentication-using-oauth/

允许我发送原始命令:

imap.SendRawCommand("AUTHENTICATE XOAUTH <base64_data>");

这显示了如何构建命令: https://developers.google.com/gmail/xoauth2_protocol

但是很难将它们放在一起。

limilabs 在这个例子中很好地组合了所有东西: http://www.limilabs.com/blog/oauth2-gmail-imap-service-account

他们有一个简洁的imap.LoginOAUTH2(userEmail, credential.Token.AccessToken); 将事情包装成一个命令。如何将其作为 Chilkat 的原始命令执行?

【问题讨论】:

    标签: oauth imap google-oauth gmail-imap chilkat


    【解决方案1】:
    const string serviceAccountEmail = "service-account-xxxxxx@developer.gserviceaccount.com";
    const string serviceAccountCertPath = @"service-xxxxxx.p12";
    const string serviceAccountCertPassword = "notasecret";
    const string userEmail = "user@domain.com";
    
    
    
            X509Certificate2 certificate = new X509Certificate2(
                serviceAccountCertPath,
                serviceAccountCertPassword,
                X509KeyStorageFlags.Exportable);
    
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                {
                    Scopes = new[] { "https://mail.google.com/" },
                    User = userEmail
                }.FromCertificate(certificate));
    
            bool success = credential.RequestAccessTokenAsync(CancellationToken.None).Result;
    
            using (Chilkat.Imap imap = new Chilkat.Imap())
            {
                imap.UnlockComponent("unlock-code");
                imap.Ssl = true;
                imap.Port = 993;
                imap.Connect("imap.gmail.com");
    
    
    
                var authString = String.Format("user={0}" + "\x001" + "auth=Bearer {1}" + "\x001" + "\x001",userEmail, credential.Token.AccessToken);
    
               var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(authString));
    
                string response = imap.SendRawCommand("AUTHENTICATE XOAUTH2 " + encoded);
    
                imap.SelectMailbox("Inbox");
                bool bUid;
                bUid = false;
                string mimeStr;
                int i;
                int n;
                n = imap.NumMessages;
                for (i = 1; i <= n; i++)
                {
    
                    //  Download the email by sequence number.
                    mimeStr = imap.FetchSingleAsMime(i, bUid);
    
    
                    Chilkat.Email chilkatEmail = new Chilkat.Email();
                    chilkatEmail.SetFromMimeText(mimeStr);
                    Console.WriteLine(chilkatEmail.Subject);
                }
    
    
                imap.CloseMailbox("Inbox");
    
                Console.ReadLine();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2016-03-22
      • 1970-01-01
      • 2013-08-01
      • 2013-07-28
      • 2015-02-07
      相关资源
      最近更新 更多