我是这个网站的新手。我正在寻找我的问题的答案。但我在本网站上看到了同样的问题。问题是here 我正在使用Windows 7.我没有在那个链接中得到答案。所以我再次问同样的问题。我想在java应用程序的浏览器中打开一个gmail帐户链接。是的我知道Desktop类中的browse()方法。问题是我可以打开gmail网站,但我需要在提供用户名和密码时直接打开指定的gmail帐户。有什么想法吗?
答案 0 :(得分: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/");