安卓的Oauth异常SMTP传输连接,采用Android 2.3版本 [英] Android Oauth Exception with SMTP Transport connect,with android version 2.3

查看:852
本文介绍了安卓的Oauth异常SMTP传输连接,采用Android 2.3版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我尝试添加新AUTH XOAUTH2 command.I后运行我的Andr​​oid项目正在使用Android设备与操作系统版本2.3,但同code是工作的罚款在Android 4.0

 公共SMTPTransport connectToSmtp(字符串主机,INT端口,USEREMAIL字符串,字符串oauthToken,布尔调试)抛出异常
{

    属性道具=新特性();
    props.put(mail.smtp.starttls.enable,真);
    props.put(mail.smtp.starttls.required,真);
    props.put(mail.smtp.sasl.enable,假);
    会议= Session.getInstance(道具);
    session.setDebug(调试);


    最后URLName unusedUrlName = NULL;
    SMTPTransport运输=新SMTPTransport(会话,unusedUrlName);
    //如果密码非空,SMTP尝试这样做AUTH LOGIN。
    最后弦乐emptyPassword = NULL;
    transport.connect(主机,端口,USEREMAIL,emptyPassword);

    byte []的响应=的String.Format(用户=%S \ 1auth =承载%S \ 1 \ 1,USEREMAIL,oauthToken).getBytes();
    响应= BASE64En coderStream.en code(响应);

    transport.issueCommand(AUTH XOAUTH2+新的String(响应),235);

    返回运输;
}
 

请检查日志

 十月3日至21日:05:08.679:W / System.err的(987):javax.mail.MessagingException的:334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ ==
10月三号至21日:05:08.679:W / System.err的(987):在com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
10月三号至21日:05:08.679:W / System.err的(987):在com.swapmeen.test.GMailOauthSender.connectToSmtp(GMailOauthSender.java:48)
10月三号至21日:05:08.679:W / System.err的(987):在com.swapmeen.test.GMailOauthSender.sendMail(GMailOauthSender.java:57)
10月三号至21日:05:08.679:W / System.err的(987):在com.swapmeen.test.MainActivity $ 1.运行(MainActivity.java:64)
 

解决方案

您收到错误

<$p$p><$c$c>eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==

德codeS到使用base64德codeR以下

  {地位:400,计划:旗手,范围:https://mail.google.com/}
 

这是什么最终含义(如隐秘的消息)是您正在使用的认证令牌已过期。您需要失效,然后得到一个新的(只需再次请求令牌)。

您无效像这样的标记:

  mAccountManager.invalidateAuthToken(com.google,mAuthenticationToken);
 

我希望这是有帮助的:)

I am getting following error when I try to run my android project after adding new AUTH XOAUTH2 command.I am using android device with os version 2.3 but same code is working fine on android 4.0

    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;
}

Please check the logs

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)

解决方案

The error you are receiving

eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==

decodes to the following using a Base64 Decoder

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

What this ends up meaning (as cryptic as the message is) is that the authentication token that you are using has expired. You need to invalidate it and then get a new one (simply request the token again).

You invalidate the token like this:

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

I hope that this is helpful :)

这篇关于安卓的Oauth异常SMTP传输连接,采用Android 2.3版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆