【发布时间】:2017-04-12 09:37:10
【问题描述】:
所以我正在尝试刷新访问令牌以使其正常工作。 当访问令牌过期时,我运行 refreshToken() 并传递 refresh_token 以从 Google 获取新的 access_token。
有时它似乎可以工作,但有时我会在此之后收到错误消息 invalid_grant。如果不重新授权访问我的 YouTube 频道,我似乎不能超过几天。
我做错了什么重要的事情?
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
$newToken = $client->getAccessToken();
//Run refreshToken() and pass in the refresh token to get a fresh access token.
$client->refreshToken($newToken['refresh_token']);
//Take old key object and replace everything except for refresh_token
$newKey = $client->getAccessToken();
$newKeyWithRefreshToken = json_decode($oldKey);
$newKeyWithRefreshToken->access_token = $newKey['access_token'];
$newKeyWithRefreshToken->token_type = $newKey['token_type'];
$newKeyWithRefreshToken->expires_in = $newKey['expires_in'];
$newKeyWithRefreshToken->created = $newKey['created'];
//save to db
DB::getInstance()->update('channel', $channelId , array(
'credentials' => json_encode($newKeyWithRefreshToken)
));
【问题讨论】:
标签: php youtube youtube-api google-oauth youtube-data-api