简体   繁体   English

无法使用IMAP连接到Gmail

[英]Unable to connect to gmail using IMAP

Im trying to use JavaMail to connect to gmail using IMAP 我正在尝试使用JavaMail使用IMAP连接到gmail

Here's a code snippet of what Im doing 这是我正在做的代码片段

public class OAuth2Authenticator {
private static final Logger logger = Logger
        .getLogger(OAuth2Authenticator.class.getName());
private static Session mSession;

public static final class OAuth2Provider extends Provider {
    private static final long serialVersionUID = 1L;

    public OAuth2Provider() {
        super("Google OAuth2 Provider", 1.0,
                "Provides the XOAUTH2 SASL Mechanism");
        put("SaslClientFactory.XOAUTH2",
                "org.scribe.examples.OAuth2SaslClientFactory");
    }
}

public static void initialize() {
    Security.addProvider(new OAuth2Provider());
}

public static IMAPStore connectToImap(String host, int port,
        String userEmail, String oauthToken, boolean debug)
        throws Exception {
    Properties props = new Properties();
    props.put("mail.imaps.sasl.enable", "true");
    props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
    props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
    Session session = Session.getInstance(props);
    session.setDebug(debug);

    final URLName unusedUrlName = null;
    IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
    final String emptyPassword = "";
    store.connect(host, port, userEmail, emptyPassword);
    return store;
}



public static synchronized void testImap(String user, String oauthToken) {
    try {

        initialize();



        IMAPStore imapStore = connectToImap("imap.gmail.com", 993, user,
                oauthToken, true);

    } catch (Exception e) {

        System.out.println("Failed");
        e.printStackTrace();
    }

}


    public static void main (String[] args){

        String username="username@gmail.com";
        String oauthToken=GoogleExample.getOAuthCode();// explained below
        testImap(username, oauthToken);
    }
}

In order to get the OauthToken, I'm using scribe like this 为了获得OauthToken,我正在使用这样的抄写员

private static final String NETWORK_NAME = "Google";
  private static final String AUTHORIZE_URL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=";
  private static final String PROTECTED_RESOURCE_URL = "https://mail.google.com/mail/feed/atom";
  private static final String SCOPE = "https://mail.google.com/mail"; 


  public static String getOAuthCode(){

      OAuthService service = new ServiceBuilder()
      .provider(GoogleApi.class)
      .apiKey("anonymous")
      .apiSecret("anonymous")
      .scope(SCOPE)
      .build();
      Scanner in =new Scanner(System.in);
      System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
      System.out.println();

      //Obtain Request token
      System.out.println("Fetching request token");
      Token requestToken = service.getRequestToken();
      System.out.println("The request token"+requestToken);
      System.out.println("Authorizing Scribe");
      System.out.println();
      System.out.println(AUTHORIZE_URL + requestToken.getToken());
      System.out.println("And paste the verifier here");
      System.out.print(">>");
      Verifier verifier = new Verifier(in.nextLine());
      System.out.println();

      System.out.println("Trading the Request Token for an Access Token...");
        Token accessToken = service.getAccessToken(requestToken, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: " + accessToken + " )");
        System.out.println();

      return accessToken.getToken();



  }

Here's a debug log of what Im getting when I run the program 这是我运行程序时得到的调试日志

Fetching request token
The request tokenToken[4/WIZGED1ommjoIwt8NQdF76IgbPio , hfXe_IPPFX1A-DPWXKOb9GKJ]
Authorizing Scribe

https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=4/WIZGED1ommjoIwt8NQdF76IgbPio
And paste the verifier here
>>VX2hIT2rRi_7822BT_WLa-81

Trading the Request Token for an Access Token...
Got the Access Token!
(if your curious it looks like this: Token[1/hKn9r0lSf1QWp0ESgn7J18PDnpE80buxvjcaGRLm9wM , -SGAHMqlD6kaxneB3evD5SMX] )

DEBUG: setDebug: JavaMail version 1.5.0
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: enable SASL
DEBUG IMAPS: SASL mechanisms allowed: XOAUTH2
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 174.26.139.84 iu5if8027627pbc.224
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
A0 OK Thats all she wrote! iu5if8027627pbc.224
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=username@gmail.com, password=<non-null>
DEBUG IMAPS: SASL authentication command trace suppressed
DEBUG IMAPS: SASL Mechanisms:
DEBUG IMAPS:  XOAUTH2
DEBUG IMAPS: 
DEBUG IMAPS: SASL client XOAUTH2
DEBUG IMAPS: SASL challenge:  :
DEBUG IMAPS: SASL callback length: 1
DEBUG IMAPS: SASL callback 0: javax.security.auth.callback.NameCallback@17b14695
DEBUG IMAPS: SASL response: user=username@gmail.comauth=Bearer 1/hKn9r0lSf1QWp0ESgn7J18PDnpE80buxvjcaGRLm9wM :
DEBUG IMAPS: SASL no response
Failed
javax.mail.AuthenticationFailedException: [ALERT] Invalid credentials (Failure)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:663)
    at javax.mail.Service.connect(Service.java:345)
    at org.scribe.examples.OAuth2Authenticator.connectToImap(OAuth2Authenticator.java:187)
    at org.scribe.examples.OAuth2Authenticator.testImap(OAuth2Authenticator.java:219)
    at org.scribe.examples.OAuth2Authenticator.main(OAuth2Authenticator.java:273)

Sorry for the lengthy post, but any insight on where I;m going wrong would be greatly appreciated. 很抱歉,冗长的帖子,但是如果我对哪里出错了有任何见解,将不胜感激。 Thanks! 谢谢!

At a guess, your token is bad. 猜想,您的令牌不好。

1/hKn9r0lSf1QWp0ESgn7J18PDnpE80buxvjcaGRLm9wM , -SGAHMqlD6kaxneB3evD5SMX

That's not how access tokens typically look. 这不是访问令牌通常的外观。 It looks more like an access token and a refresh token separated by a comma. 它看起来更像是一个访问令牌和一个刷新令牌,用逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM