谷歌 API - 没有refresh_token


google api - no refresh_token

我对refresh_token有问题。我完全不明白。我读到我只是在登录后第一次收到这个令牌,之后refresh_token不会返回。第一种方法是初始化 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));             
        },

接下来,当用户单击"连接到YouTube"按钮时,响应代码将发送到"/connect-google"路由:

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();
        }
    });
});
},

然后在PHP中我得到了:

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

但在access_token我没有得到refresh_token。为什么?是因为我使用"帖子"吗?

你可以尝试这个,每次都得到refresh_token。

$googleClient->setApprovalPrompt('force');

然后,一旦完成构建应用程序,就可以删除此行。希望对您有所帮助。