google api - 没有refresh_token

时间:2016-07-08 07:31:06

标签: php google-api-client

我遇到了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。为什么?是因为我使用' postmessage' ?

1 个答案:

答案 0 :(得分:1)

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

$googleClient->setApprovalPrompt('force');

然后,在完成构建应用程序后,您可以删除此行。希望能帮助到你。