以下https://developers.google.com/identity/sign-in/web/server-side-flow從JavaScript獲取授權碼並將其傳遞到服務器端後,我們確實獲得了訪問令牌(和ID令牌),但不是需要刷新令牌。獲取Gmail API訪問和ID令牌,但刷新令牌爲NULL
有很多帖子,但還不能解決它。
任何建議如何獲取刷新令牌?
謝謝!
private String getResponseToken(GoogleClientSecrets clientSecrets,
String authCode) throws IOException {
try {
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
// "https://accounts.google.com/o/oauth2/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode, //NOTE: was received from JavaScript client
"postmessage" //TODO: what's this?
).execute();
String accessToken = tokenResponse.getAccessToken();
String idToken = tokenResponse.getIdToken();
//TODO: not getting a refresh token... why?!
String refreshToken = tokenResponse.getRefreshToken();
Boolean hasRefreshToken = new Boolean(!(refreshToken == null));
LOGGER.warn("received refresh token: {}", hasRefreshToken);
LOGGER.debug("accessToken: {}, refreshToken: {}, idToken: {}", accessToken, refreshToken, idToken);
return accessToken;
}catch (TokenResponseException tre){...}
的可能重複https://stackoverflow.com/questions/10631042/how-to-generate-access-令牌使用刷新令牌通過谷歌驅動器api – noogui
看起來不像重複。實際上,尋找在這篇文章中完成的第一步:「我已完成授權步驟並獲得訪問令牌和刷新令牌。」這就是我們需要的:獲得刷新令牌。其他人在該帖子的底部指出,他在PHP中使用$ client-> setAccessType(「offline」);)。問題是如何使用Java API獲取初始刷新令牌? – Roy