谷歌javascript登录api:没有离线访问 [英] Google javascript sign-in api: no offline access
问题描述
我试图为Google服务器端应用实施Google Sign-In,如Google文档所示: Google登录服务器端应用程序,但同意窗口从不要求脱机访问。选择一个用户后,它会关闭并调用登录处理函数。
因此,当我得到一次性代码并将它发送到服务器时,我不能将它换为刷新令牌,仅用于访问和id令牌。
这是我的客户端代码:
在HTML文件中:
< script src =https ://apis.google.com/js/platform.js?onload = initasync defer>< / script>
Javascript代码:
var auth2;
函数init(){
gapi.load('auth2',function(){
auth2 = gapi.auth2.init({
client_id:'< my client id> ; .apps.googleusercontent.com',
范围:'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);
});
}
函数onSignIn(authResult){
if(authResult ['code']){
//发送代码到服务器
}
$ / code>
Google控制台中的项目包含一个带有相关javascript来源的Web客户端凭证,
我应该如何处理迫使同意窗口要求离线访问?
通过添加参数
'approval_prompt':'force'$ c $,我可以强制脱机提示c>到 auth2.grantOfflineAccess
函数调用。例如
auth2.grantOfflineAccess({'redirect_uri':'postmessage','approval_prompt':'force'})。then(onSignIn);
I'm trying to implement Google Sign-In for server-side apps as shown in Google documentation: Google Sign-In for server-side apps, but the consent window never asks for offline access. After selecting a user it just closes and calls the sign in handler function.
As a result, when I get the one time code and send it to the server, I cannot exchange it for a refresh token, only for access and id tokens.
Here is my client code:
In the HTML file:
<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>
Javascript code:
var auth2;
function init() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '<my client id>.apps.googleusercontent.com',
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);
});
}
function onSignIn(authResult) {
if (authResult['code']) {
// Send the code to the server
}
}
The project in Google console contains a web client credential with the relevant javascript origins and no Authorized redirect URIs.
What should I do to force the consent window to ask for offline access?
解决方案 I'm able to force the prompt for offline, by adding the param 'approval_prompt' : 'force'
to auth2.grantOfflineAccess
function call.
E.g.
auth2.grantOfflineAccess({'redirect_uri' : 'postmessage', 'approval_prompt' : 'force'}).then(onSignIn);
这篇关于谷歌javascript登录api:没有离线访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!