好吧,請注意以下幾點注意事項: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/");
你想試試郵件嗎?或其他喜歡谷歌驅動器的東西?如果您只想處理郵件,那麼您可以使用IMAP或POP3工作。 –
我很確定這是不可能的。 [在這個鏈接](http://www.marcofolio.net/tips/automatic_sign_into_gmail_using_a_bookmark.html)有一個可能用於工作的描述,但它不適用於我。如果用戶向您提供他的Google憑據,這本質上是不安全的。您可以直接在您的應用程序中使用Google API來使用OAuth2,因此您絕不會擁有Google用戶的憑據。 – jlordo