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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With