2011-05-23 4 views
1

J'essaie de lire Gmail dans-boîte en utilisant IMAP avec OAuth. Lorsque vous utilisez la méthode principale de base, tout fonctionne très bien:Secret vide retourné dans Google Gmail OAuth

private static final String SCOPE = "https://mail.google.com/"; 

private static final String CONSUMER_KEY = "www.******.com"; 
private static final String CONSUMER_SECRET = "******"; 
private static final String USER_EMAIL = "******"; 

public static void main(String[] args) throws Exception { 

    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 

    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 
    OAuthSigner signer = new OAuthHmacSha1Signer(); 

    GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer); 

    oauthParameters.setScope(SCOPE); 

    oauthHelper.getUnauthorizedRequestToken(oauthParameters); 

    String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters); 
    System.out.println(requestUrl); 
    System.out.println("Please visit the URL above to authorize your OAuth " 
      + "request token. Once that is complete, press any key to " 
      + "continue..."); 
    System.in.read(); 

    String token = oauthHelper.getAccessToken(oauthParameters); 
    System.out.println("OAuth Access Token: " + token); 
    System.out.println(); 

    //IMAP 
    XoauthAuthenticator.initialize(); 

    IMAPSSLStore imapSslStore = XoauthAuthenticator.connectToImap("imap.googlemail.com", 
      993, 
      USER_EMAIL, 
      oauthParameters.getOAuthToken(), 
      oauthParameters.getOAuthTokenSecret(), 
      new OAuthConsumer(null, CONSUMER_KEY, CONSUMER_SECRET, null), 
      true); 
    System.out.println("Successfully authenticated to IMAP.\n"); 
} 

mais, quand je me déplace vers 3 jambe OAuth, je reviens secret de jeton d'accès vide, ce code est contrôleur:

private static final String CALLBACK = "http://www.*******.com/oauthback.htm"; 

private static final String SCOPE = "https://mail.google.com/"; 
private static final String CONSUMER_KEY = "www.******.com"; 
private static final String CONSUMER_SECRET = "******"; 
private static final String USER_EMAIL = "******"; 

@RequestMapping("/oauth.htm") 
public void oauth(HttpServletResponse response, HttpSession session) throws OAuthException, IOException, 
     Base64DecoderException, InvalidKeySpecException, NoSuchAlgorithmException { 
    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthCallback(CALLBACK); 
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 
    oauthParameters.setScope(SCOPE); 

    OAuthSigner signer = new OAuthHmacSha1Signer(); 
    GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer); 

    oauthHelper.getUnauthorizedRequestToken(oauthParameters); 

    String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters); 
    System.out.println(requestUrl); 

    String accessTokenSecret = oauthParameters.getOAuthTokenSecret(); 
    System.out.println("OAuth Access Token's Secret: " + accessTokenSecret); 
    session.setAttribute("accessTokenSecret", accessTokenSecret); 

    response.sendRedirect(requestUrl); 
} 

@RequestMapping("/oauthback.htm") 
public String oauthback(HttpServletRequest request, HttpSession session, Model model) throws Exception { 
    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 

    GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer()); 
    oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters); 

    String accessToken = oauthParameters.getOAuthToken(); 
    System.out.println("OAuth Access Token: " + accessToken); 

    String accessTokenSecret = oauthParameters.getOAuthTokenSecret(); 
    System.out.println("OAuth Access Token's Secret: " + accessTokenSecret); 

    //IMAP 
    XoauthAuthenticator.initialize(); 

    IMAPSSLStore imapSslStore = XoauthAuthenticator.connectToImap("imap.googlemail.com", 
      993, 
      USER_EMAIL, 
      accessToken, 
      accessTokenSecret, 
      new OAuthConsumer(CALLBACK, CONSUMER_KEY, CONSUMER_SECRET, null), 
      true); 
    System.out.println("Successfully authenticated to IMAP.\n"); 
    model.addAttribute("msg", "accessToken: " + accessToken + " accessTokenSecret: " + accessTokenSecret); 

    return "errorView"; 
} 

Qu'est-ce que Je fais mal? Je parie que c'est quelque chose de simple - mais je suis juste essayer trop longtemps :)

Répondre

2

... Résolu dans le cas où une personne a besoin:

@RequestMapping("/oauth.htm") 
public void oauth(HttpServletResponse response, HttpSession session) throws OAuthException, IOException, 
     Base64DecoderException, InvalidKeySpecException, NoSuchAlgorithmException { 
    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthCallback(CALLBACK); 
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 
    oauthParameters.setScope(SCOPE); 

    oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH); 


    OAuthSigner signer = new OAuthHmacSha1Signer(); 
    GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer); 

    oauthHelper.getUnauthorizedRequestToken(oauthParameters); 

    String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters); 
    System.out.println(requestUrl); 

    String accessTokenSecret = oauthParameters.getOAuthTokenSecret(); 
    System.out.println("OAuth Access Token's Secret: " + accessTokenSecret); 
    session.setAttribute("accessTokenSecret", accessTokenSecret); 

    response.sendRedirect(requestUrl); 
} 

@RequestMapping("/oauthback.htm") 
public String oauthback(HttpServletRequest request, HttpSession session, Model model) throws Exception { 
    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
    oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 

    System.out.println("Query String: " + request.getQueryString()); 

    GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer()); 
    oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters); 

    oauthParameters.setOAuthTokenSecret((String) session.getAttribute("accessTokenSecret")); 

    String token = oauthHelper.getAccessToken(oauthParameters); 
    System.out.println("OAuth Token: " + token); 

    String accessToken = oauthParameters.getOAuthToken(); 
    System.out.println("OAuth Access Token: " + accessToken); 

    String accessTokenSecret = oauthParameters.getOAuthTokenSecret(); 
    System.out.println("OAuth Access Token's Secret: " + accessTokenSecret); 

    //IMAP 
    XoauthAuthenticator.initialize(); 

    IMAPSSLStore imapSslStore = XoauthAuthenticator.connectToImap("imap.googlemail.com", 
      993, 
      USER_EMAIL, 
      accessToken, 
      accessTokenSecret, 
      new OAuthConsumer(CALLBACK, CONSUMER_KEY, CONSUMER_SECRET, null), 
      true); 
    System.out.println("Successfully authenticated to IMAP.\n"); 
    model.addAttribute("msg", "accessToken: " + accessToken + " accessTokenSecret: " + accessTokenSecret); 

    return "errorView"; 
} 
+0

Je voudrais mettre en œuvre cette solution peut vous conseiller où s'il vous plaît vous avez trouvé les pots pour cela. Deux ans après - est-ce toujours la meilleure solution? – Dan

Questions connexes