有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用通过JAVA代码生成的令牌时,oauth的凭据无效(失败)

我试图使用oauth访问我的收件箱,当我使用使用JAVA代码生成的令牌时,总是会出现无效凭据错误。我正在使用IMAP并传递oauth令牌以获得身份验证。但是,当我使用python生成令牌并在代码中使用该令牌时,它可以很好地从我的收件箱中获取电子邮件

以下是使用IMAP的代码片段:

public static IMAPSSLStore connectToImap(String host,
                                          int port,
                                          String userEmail,
                                          String oauthToken,
                                          String oauthTokenSecret,
                                          OAuthConsumer consumer,
                                          boolean debug) throws Exception {
   Properties props = new Properties();
   props.put("mail.imaps.sasl.enable", "true");
   props.put("mail.imaps.sasl.mechanisms", "XOAUTH");
   props.put(XoauthSaslClientFactory.OAUTH_TOKEN_PROP,
             oauthToken);
   props.put(XoauthSaslClientFactory.OAUTH_TOKEN_SECRET_PROP,
             oauthTokenSecret);
   props.put(XoauthSaslClientFactory.CONSUMER_KEY_PROP,
             consumer.consumerKey);
   props.put(XoauthSaslClientFactory.CONSUMER_SECRET_PROP,
             consumer.consumerSecret);
   Session session = Session.getInstance(props);
   session.setDebug(debug);

   final URLName unusedUrlName = null;
   IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
   final String emptyPassword = "anonymous";

   store.connect(host, port, userEmail, emptyPassword);
   return store;
 }

这是错误日志-

DEBUG: setDebug: JavaMail version 1.3.3
1
2
DEBUG: mail.imap.fetchsize: 16384
DEBUG: enable SASL
DEBUG: SASL mechanisms allowed: XOAUTH
3
4
* OK Gimap ready for requests from 122.179.85.133 kn10if6680532igc.14
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
A0 OK Thats all she wrote! kn10if6680532igc.14
IMAP DEBUG: AUTH: XOAUTH
DEBUG: protocolConnect login, host=imap.googlemail.com, user=me@mydomain.com, password=<non-null>
IMAP SASL DEBUG: Mechanisms: XOAUTH
IMAP SASL DEBUG: SASL client XOAUTH
A1 AUTHENTICATE XOAUTH
+ 
IMAP SASL DEBUG: challenge:  :
IMAP SASL DEBUG: callback length: 1
IMAP SASL DEBUG: callback 0: javax.security.auth.callback.NameCallback@d1e832
IMAP SASL DEBUG: response: GET https://mail.google.com/mail/b/me@mydomain.com/imap/ oauth_signature_method="HMAC-SHA1",oauth_token="1%2Furn42qGc5mpD3BwVvJMzeVkHl_9iVRCZvaSOYmKNH5A",oauth_consumer_key="mydomain.com",oauth_timestamp="1329301212",oauth_nonce="629121335212995",oauth_version="1.0",oauth_signature="7O1YHXywiqOX8XzSAd%2BzbmDoBVw%3D" :
R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21haWwvYi9wZXRlckBhMm9tb2JpbGUuY29tL2ltYXAvIG9hdXRoX3NpZ25hdHVyZV9tZXRob2Q9IkhNQUMtU0hBMSIsb2F1dGhfdG9rZW49IjElMkZ1cm40MnFHYzVtcEQzQndWdkpNemVWa0hsXzlpVlJDWnZhU09ZbUtOSDVBIixvYXV0aF9jb25zdW1lcl9rZXk9ImEyb21vYmlsZS5jb20iLG9hdXRoX3RpbWVzdGFtcD0iMTMyOTMwMTIxMiIsb2F1dGhfbm9uY2U9IjYyOTEyMTMzNTIxMjk5NSIsb2F1dGhfdmVyc2lvbj0iMS4wIixvYXV0aF9zaWduYXR1cmU9IjdPMVlIWHl3aXFPWDhYelNBZCUyQnpibURvQlZ3JTNEIg==
5
DEBUG: connection available -- size: 1
A2 EXAMINE INBOX
A1 NO [ALERT] Invalid credentials (Failure)
A2 BAD Unknown command r5if6689112igo.50
A3 LOGOUT
* BYE Logout Requested r5if6689112igo.50
A3 OK Quoth the raven, nevermore... r5if6689112igo.50
Exception in thread "main" javax.mail.MessagingException: A2 BAD Unknown command r5if6689112igo.50;
  nested exception is:
    com.sun.mail.iap.BadCommandException: A2 BAD Unknown command r5if6689112igo.50
    at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:820)
    at com.a2o.service.collaboration.common.XoauthAuthenticator.getMessages(XoauthAuthenticator.java:238)
    at com.a2o.service.collaboration.common.XoauthAuthenticator.main(XoauthAuthenticator.java:205)

有没有办法从JAVA代码中正确生成令牌。我的实际需求是使用我的iPad客户端生成令牌,并在服务器中使用该令牌获取电子邮件。任何帮助都是非常感谢的


共 (1) 个答案