2013-03-21 60 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