【问题标题】:java :direct gmail urljava : 直接 gmail 网址
【发布时间】:2012-11-15 04:00:29
【问题描述】:

我对这个网站完全陌生。我正在寻找我的问题的答案。但我看到这个网站上有人问过同样的问题。问题是here 我正在使用 Windows 7。我没有在那个链接中得到答案..所以我再次问同样的问题。我想从 Java 应用程序在浏览器中打开一个 gmail 帐户链接。是的,我确实知道 Desktop 类中的 browse() 方法。问题是我可以打开 gmail 网站,但我需要在提供用户名和密码的情况下直接打开指定的 gmail 帐户。有什么想法吗?

【问题讨论】:

  • 您是否只想处理邮件?或其他类似谷歌驱动器的东西?如果您只想处理邮件,那么您可以使用 IMAP 或 POP3。
  • 我很确定这是不可能的。 In this link 有一个描述可能曾经有效,但对我不起作用。如果用户向您提供他的 Google 凭据,则本质上是不安全的。您可以通过 OAuth2 在您的应用程序中直接使用 Google API,因此您永远不会拥有用户的 Google 凭据。

标签: java windows email gmail


【解决方案1】:

好的,因此请注意以下几点:1. 我上次使用 Google API 是在旧版本中,所以现在可能会大不相同,2. 此代码未经测试,我是只是部分从记忆中写出来,部分从我的一个旧项目中写出来。把它想象成更像是伪代码,并且 3. 如果这是偶然的工作,这是一个非常肮脏的解决方案。希望这可以帮助您找到更好的 API 方法。

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey( [insert consumer key here] );

oauthParameters.setOAuthConsumerSecret( [insert consumer secret here] );
OAuthSigner signer = new OAuthHmacSha1Signer();

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

oauthParameters.setScope("https://mail.google.com/mail");  //no clue if this is a valid scope or not

oauthHelper.getUnauthorizedRequestToken(oauthParameters);
String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);

Desktop desktop = Desktop.getDesktop();
URI url;
url = new URI(requestUrl);
//this will make the user log in to authorize your app
desktop.browse(url);

//auth token response from Google, you can use this to authenticate your app if there are other requests you want to make against the user account
String token = oauthHelper.getAccessToken(oauthParameters);

//since you made sure the user is logged into their account to authorize your app, their gmail can now just be opened.  Yes, very dirty.  I know. (if it all works)
desktop.browse("https://www.gmail.com/");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2011-07-17
    • 2021-08-16
    • 2011-05-19
    • 2023-04-03
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多