我使用Google API获取访问权限并刷新令牌以进行离线访问,但刷新令牌始终为空。
authorizationCode来自客户端,因此我将其与服务器上的令牌交换:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
clientId,
clientSecret,
scopes)
.setAccessType("offline")
.build();
GoogleTokenResponse response = flow
.newTokenRequest(authorizationCode)
.setRedirectUri(redirectUri)
.execute();
// response has access_token and id_token. don't see any refresh token here
return flow.createAndStoreCredential(response, null);
// this returns Credential with refreshToken = null
客户代码:
$(document).ready(function() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '<my client id>',
// Scopes to request in addition to 'profile' and 'email'
scope: 'https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appfolder'
});
});
$('#signinButton').click(function() {
auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(onSignIn);
});
});
// the onSignIn function sends the one time code (i.e. authResult['code']) to the server
我在这里做错了吗?
我看到了这个问题:Google OAuth: can't get refresh token with authorization code但它并没有真正回答我的问题,而且我不知道只有在用户首次登录时才会创建刷新令牌。
修改:
我认为这实际上是客户端问题,因为同意窗口并不要求离线访问。将根据新问题的结果更新此问题:Google javascript sign-in api: no offline access