Google javascript login api: no offline access

I am trying to implement Google login for server-side applications, as shown in the Google documentation: Google login for server applications , but the consent window never asks for offline access. After selecting a user, it simply closes and calls the function of the icon handler.

As a result, when I receive one time code and send it to the server, I cannot exchange it for the refresh token, only for access tokens and identifiers.

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 the Google console contains web client credentials with corresponding javascript reasons and without authorized redirect URIs.

What should I do to make the consent window request offline access ?

+5
source share
1 answer

I can force a prompt for offline by adding a call to the function 'approval_prompt' : 'force' to auth2.grantOfflineAccess .

eg.

auth2.grantOfflineAccess({'redirect_uri' : 'postmessage', 'approval_prompt' : 'force'}).then(onSignIn);

+7
source

Source: https://habr.com/ru/post/1241252/


All Articles