Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google javascript sign-in api: no offline access

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?

like image 647
burgi Avatar asked Jan 22 '16 15:01

burgi


1 Answers

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);

like image 76
Mael Avatar answered Oct 17 '22 09:10

Mael