예제 #1
0
        private string GetAccessToken(MailBox mbox)
        {
            var serviceType = (AuthorizationServiceType)mbox.ServiceType;

            switch (serviceType)
            {
                case AuthorizationServiceType.Google:
                    var grantedAccess = new GoogleOAuth2Authorization(log)
                        .RequestAccessToken(mbox.RefreshToken);

                    if (grantedAccess != null)
                        return grantedAccess.AccessToken;
                    break;
            }

            return "";
        }
예제 #2
0
        public static void AuthenticateImapGoogleOAuth2(this Imap4Client imap, MailBox account, ILogger log = null)
        {
            if (log == null)
                log = new NullLogger();

            var auth = new GoogleOAuth2Authorization(log);
            var grantedAccess = auth.RequestAccessToken(account.RefreshToken);
            if (grantedAccess == null)
                throw new DotNetOpenAuth.Messaging.ProtocolException("Access denied");
            log.Info("IMAP SSL connecting to {0}", account.EMail);
            imap.ConnectSsl(account.Server, account.Port);

            log.Info("IMAP connecting OK {0}", account.EMail);

            log.Info("IMAP logging to {0} via OAuth 2.0", account.EMail);
            imap.LoginOAuth2(account.Account, grantedAccess.AccessToken);
            log.Info("IMAP logged to {0} via OAuth 2.0", account.EMail);
        }