Google Gmail OAuth 中返回空机密

发布于 2024-11-09 12:56:58 字数 4480 浏览 2 评论 0原文

我正在尝试使用 IMAP 和 OAuth 读取 Gmail 收件箱。 使用基本的 main 方法时,一切正常:

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

但是,当我转向 3 条 OAuth 时,我得到空的访问令牌密钥,这是控制器代码:

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

我做错了什么?我打赌这很简单 - 但我只是尝试了太久:)

I am trying to read Gmail in-box using IMAP with OAuth.
When using basic main method, everything works fine:

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

but, when I move towards 3 leg OAuth, I am getting back empty access token secret, this is controller code:

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

What I am doing wrong? I bet this is something simple - but I am just trying for too long :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-11-16 12:56:58

解决了...以防万一有人需要它:

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

Solved... in case someone needs it:

@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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文