簡體   English   中英

使用oauth2連接到用戶電子郵件

[英]Connecting to user email with oauth2

我正在嘗試使用Oauth2通過用戶帳戶(無需他輸入用戶名和密碼)發送電子郵件。

這是我獲取訪問令牌的代碼:

String SCOPES =  "https://mail.google.com/";
token = GoogleAuthUtil.getToken(m_context,SystemUtills.getUsername(m_context)+"@gmail.com","oauth2:" + SCOPES);

這很好用,並給了我一個令牌。

現在,我嘗試像這樣連接到imap和smtp:

public class OAuth2Authenticator   extends AsyncTask<String, Void, String> {
private static final Logger logger =Logger.getLogger(OAuth2Authenticator.class.getName());
private static String user_email;
public static final class OAuth2Provider extends Provider {
private static final long serialVersionUID = 1L;

public OAuth2Provider() {
  super("Google OAuth2 Provider", 1.0,
        "Provides the XOAUTH2 SASL Mechanism");
  put("SaslClientFactory.XOAUTH2",
      "com.google.code.samples.oauth2.OAuth2SaslClientFactory");

}
}

 public static void initialize() {
Security.addProvider(new OAuth2Provider());
}

public static IMAPStore connectToImap(String host,
                                    int port,
                                    String userEmail,
                                    String oauthToken,
                                    boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);

final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
final String emptyPassword = "";
store.connect(host, port, userEmail, emptyPassword);
return store;
}


public static SMTPTransport connectToSmtp(String host,
                                        int port,
                                        String userEmail,
                                        String oauthToken,
                                        boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);

final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = "";
transport.connect(host, port, userEmail, emptyPassword);

return transport;
}

@Override
protected String doInBackground(String... arg0) 
{
    try
    {
        IMAPStore imapStore = connectToImap("imap.gmail.com", 993, AppSettings.user_email,AppSettings.getAccessToken(), true);
        SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",587,AppSettings.user_email,AppSettings.getAccessToken(),true);
        return "SUC";
    }catch (Exception e) {
        e.printStackTrace();
        return "FLD";
    }
}

在調用類中,我使用以下方法調用連接:

new AsyncGetAccessToken().execute();

    OAuth2Authenticator.initialize();
    try{

        new OAuth2Authenticator().execute();
    }catch (Exception e) {
        e.printStackTrace();
    }

對於smtp(調試協商:

04-01 07:57:08.770: I/System.out(31190): DEBUG: setDebug: JavaMail version 1.4.1
04-01 07:57:37.890: I/System.out(31190): DEBUG SMTP: useEhlo true, useAuth false
04-01 07:57:37.900: I/System.out(31190): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587,  isSSL false
04-01 07:57:38.150: I/System.out(31190): 220 mx.google.com ESMTP q41sm37770244eez.7 - gsmtp
04-01 07:57:38.150: I/System.out(31190): DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
04-01 07:57:38.155: I/System.out(31190): EHLO localhost
04-01 07:57:38.310: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170]
04-01 07:57:38.310: I/System.out(31190): 250-SIZE 35882577
04-01 07:57:38.320: I/System.out(31190): 250-8BITMIME
04-01 07:57:38.320: I/System.out(31190): 250-STARTTLS
04-01 07:57:38.320: I/System.out(31190): 250-ENHANCEDSTATUSCODES
04-01 07:57:38.320: I/System.out(31190): 250 CHUNKING
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577"
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg ""
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "STARTTLS", arg ""
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg ""
04-01 07:57:38.345: I/System.out(31190): STARTTLS
04-01 07:57:38.440: I/System.out(31190): 220 2.0.0 Ready to start TLS
04-01 07:57:39.115: I/System.out(31190): EHLO localhost
04-01 07:57:39.220: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170]
04-01 07:57:39.220: I/System.out(31190): 250-SIZE 35882577
04-01 07:57:39.220: I/System.out(31190): 250-8BITMIME
04-01 07:57:39.225: I/System.out(31190): 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
04-01 07:57:39.225: I/System.out(31190): 250-ENHANCEDSTATUSCODES
04-01 07:57:39.225: I/System.out(31190): 250 CHUNKING
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577"
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg ""
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg ""
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Attempt to authenticate
04-01 07:57:39.250: I/System.out(31190): AUTH LOGIN
04-01 07:57:39.350: I/System.out(31190): 334 VXNlcm5hbWU6
04-01 07:57:39.355: I/System.out(31190): bWljaGFlbC5hc2FyYWZAZ21haWwuY29t
04-01 07:57:39.460: I/System.out(31190): 334 UGFzc3dvcmQ6
04-01 07:57:39.470: I/System.out(31190): 
04-01 07:57:39.990: I/System.out(31190): 535-5.7.8 Username and Password not accepted. Learn more at
04-01 07:57:39.990: I/System.out(31190): 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257    q41sm37770244eez.7 - gsmtp

還有一個例外:

04-01 08:00:57.860: W/System.err(31190): javax.mail.AuthenticationFailedException
04-01 08:00:57.880: W/System.err(31190):        at javax.mail.Service.connect(Service.java:319)
04-01 08:00:57.885: W/System.err(31190):        at com.my_android_assistant.OAUTH.OAuth2Authenticator.connectToSmtp(OAuth2Authenticator.java:128)

它不使用OAuth2提供程序,因為服務器會播發LOGIN,因此JavaMail首先選擇它。 您需要禁用LOGIN和PLAIN機制以強制其使用SASL並選擇XOAUTH2。 將“ mail.imaps.auth.login.disable”和“ mail.imaps.auth.plain.disable”設置為“ true”。

另外,您可能想嘗試JavaMail 1.5.2中內置OAuth2支持 SNAPSHOT版本已發布。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM