2012-11-27 37 views
3

我對這個網站完全陌生。我正在爲我的問題尋找答案。但是我看到了這個網站中某個人詢問的同樣的問題。問題是here 我正在使用Windows 7.我沒有得到答案,在那個鏈接..所以我再次問同樣的問題。我想從一個Java應用程序的瀏覽器中打開一個Gmail帳戶鏈接。是的,我確實知道Desktop類中的browse()方法。問題是我可以打開Gmail網站,但我需要直接打開指定的Gmail帳戶,同時提供用戶名和密碼。有任何想法嗎?java:d​​irect gmail url

+0

你想試試郵件嗎?或其他喜歡谷歌驅動器的東西?如果您只想處理郵件,那麼您可以使用IMAP或POP3工作。 –

+4

我很確定這是不可能的。 [在這個鏈接](http://www.marcofolio.net/tips/automatic_sign_into_gmail_using_a_bookmark.html)有一個可能用於工作的描述,但它不適用於我。如果用戶向您提供他的Google憑據,這本質上是不安全的。您可以直接在您的應用程序中使用Google API來使用OAuth2,因此您絕不會擁有Google用戶的憑據。 – jlordo

回答

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