2013-03-21 64 views
7

我收到以下錯誤,當我嘗試在添加新的AUTH XOAUTH2命令後運行我的Android項目。我使用Android設備與操作系統版本2.3,但相同的代碼工作正常在Android 4.0Android Oauth異常與SMTP傳輸連接,與Android版本2.3

public 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", "false"); 
    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 = null; 
    transport.connect(host, port, userEmail, emptyPassword); 

    byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail,oauthToken).getBytes(); 
    response = BASE64EncoderStream.encode(response); 

    transport.issueCommand("AUTH XOAUTH2 " + new String(response),235); 

    return transport; 
} 

請檢查日誌

03-21 10:05:08.679: W/System.err(987): javax.mail.MessagingException: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== 
03-21 10:05:08.679: W/System.err(987): at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481) 
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.GMailOauthSender.connectToSmtp(GMailOauthSender.java:48) 
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.GMailOauthSender.sendMail(GMailOauthSender.java:57) 
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.MainActivity$1.run(MainActivity.java:64) 
+0

餘米也越來越例外......通過掃描在issueCommand發現錯誤()傳輸的方法。任何人都會建議谷歌賬戶應該有什麼命令和期望 – 2013-05-17 08:46:17

+0

嗨,你們中的任何一個人都能做到這一點嗎? – 2013-10-22 20:17:48

回答

5

錯誤您收到

eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== 

解碼以使用base64解碼器

{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"} 

什麼這最終意味着(如隱祕的消息),以下是您所使用的認證令牌已過期。你需要使它無效,然後得到一個新的(只需再次請求令牌)。

您無效像這樣的標記:

mAccountManager.invalidateAuthToken("com.google", mAuthenticationToken); 

我希望這是有幫助:)

+1

我做了你發佈的內容,但是我的應用獲取了新的令牌,但是我仍然收到此消息... – Rohan 2014-03-04 22:31:03

+0

小的更新,它應該是「com.google」而不是「con.google」 – 2014-03-06 00:45:43