michal
michal

Reputation: 428

google api - no refresh_token

I have a problem with refresh_token. I don't get it at all. I read that I get this token only first time after login, after that refresh_token won't be returned. First method is initialize gapi:

initGp: function() {

            requirejs(['google'],
            function   (gapi) {

               gapi.load('auth2', function() {
                this.auth2 = gapi.auth2.init({
                        client_id: 'client_id',
                        scope: 'https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'
                    });
                }.bind(this));
            }.bind(this));             
        },

next when user click on button "Connect to YouTube" the response code is send to '/connect-google' route:

connectGp: function() {

var self = this;

this.auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(

function(response) {
    $('.gp-button').addClass('loading');
    var vars = {
        code: response.code
    };
    $.ajax({
        url: myUrl+"/connect-google",
        data: vars,
        type: "GET",
        success: function(response) {
             window.location.reload();
        }
    });
});
},

then in PHP I've got:

$googleClient = new \Google_Client();
$googleClient->setClientId($client_id);
$googleClient->setClientSecret($client_secret);
$googleClient->setRedirectUri('postmessage');
$googleClient->setAccessType("offline");
$googleClient->setScopes($scopes);


$token = $googleClient->authenticate($code);
$access_token = $googleClient->getAccessToken();
var_dump($access_token);

but in access_token I don't receive refresh_token. Why ? is it beacuse I'm using 'postmessage' ?

Upvotes: 3

Views: 141

Answers (1)

apadana
apadana

Reputation: 14700

You can try this and get refresh_token every time.

$googleClient->setApprovalPrompt('force');

Then once you completed building your application you can remove this line. Hope it helps.

Upvotes: 1

Related Questions