Android Oauth异常,SMTP传输连接,Android版本2.3
在添加新的AUTH XOAUTH2命令后尝试运行android项目时,我遇到以下错误。我使用的是操作系统版本为2.3的android设备,但相同的代码在android 4.0上运行良好Android Oauth异常,SMTP传输连接,Android版本2.3,android,oauth,gmail,oauth-2.0,Android,Oauth,Gmail,Oauth 2.0,在添加新的AUTH XOAUTH2命令后尝试运行android项目时,我遇到以下错误。我使用的是操作系统版本为2.3的android设备,但相同的代码在android 4.0上运行良好 public SMTPTransport connectToSmtp(String host, int port, String userEmail,String oauthToken, boolean debug) throws Exception { Properties props =
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)
您收到的错误
eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
使用Base64解码器解码到以下内容
{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}
这最终意味着(与消息一样神秘)您正在使用的身份验证令牌已过期。您需要使它失效,然后获得一个新的(只需再次请求令牌)
您可以如下方式使令牌无效:
mAccountManager.invalidateAuthToken("com.google", mAuthenticationToken);
我希望这会有所帮助:)我也遇到了例外。。。通过扫描,在issueCommand()传输方法中发现错误。有人建议google accountsHi应该有什么命令和期望吗?你们中的任何一个人有没有这样做过?我做了你们发布的,但我的应用程序得到了一个新的令牌,但我仍然收到这条消息…小更新,它应该是“com.google”而不是“con.google”