我对这个网站完全陌生。我正在寻找我的问题的答案。但我看到这个网站上有人问过同样的问题。问题在 这里 ,我使用的是 Windows 7。我没有在那个链接中得到答案..所以我再次问同样的问题。我想从 Java 应用程序在浏览器中打开一个 gmail 帐户链接。是的,我确实知道 Desktop 类中的 browse() 方法。问题是我可以打开gmail网站,但我需要在提供用户名和密码的情况下直接打开指定的gmail帐户。有任何想法吗?
问问题
259 次
1 回答
0
好的,因此请注意以下几点: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/");
于 2012-12-13T20:24:48.853 回答