无法刷新访问令牌:响应是“unauthorized_client”

时间:2017-01-08 15:51:25

标签: php oauth-2.0 google-calendar-api google-oauth2

首先,我已经阅读了其他与我在SO上看过的类似主题的问题。那里的解决方案往往存在使用从其他地方复制的凭证的问题,所以我认为它们不适用。

我正在开发一个将数据同步到Google日历的PHP网络应用。 OAuth流程完美地开始 - 我获得一个令牌,将其交换为访问令牌,并且可以成功调用API。我存储访问令牌,其中包括刷新令牌作为子项,以供将来参考。

但是,一旦令牌过期,事情就会开始出错。调用授权API函数不会返回任何错误,但是当我尝试从日历服务获取数据时,我收到错误“unauthorized_client”。

如果我尝试刷新令牌,使用Google PHP库中的fetchAccessTokenWithRefreshToken调用,我会得到相同的响应。传递的RefreshToken与先前调用中返回的相同,并且我用来发送调用的客户端对象使用与之前相同的key / secret / access_token设置,所以我真的不确定发生了什么。检查我的Google设置后,该应用仍会按授权列出。

我可以发布一些代码,希望我犯了一个菜鸟错误,但我想这更有可能是某个地方的呼叫没有被正确构建 - 或者我错误地理解了OAuth机制。任何人都可以指出一个明显的代码错误,或者从这里给我如何调试的想法?调用是由PHP库在服务器端进行的,所以我无法使用浏览器检查器检查它们 - 我想知道设置Fiddler或类似的吗?

代码示例:

    $client = getClient($client_id, $client_secret);

    try {
        $client->setAccessToken($access_token);
        $authReturn = $client->authorize();
    } catch (\Exception $e) {
        // Error handling code omitted for brevity
    }

    $service = new \Google_Service_Calendar($client);
    try {
        $calendar_list = $service->calendarList->listCalendarList();
    }
    } catch (\Google_Service_Exception $gse) {
        // unauthorized_client exception firing here
    }

getClient函数是:

function getClient($client_id = null, $client_secret = null, $redirect_url = null) {
$scope = implode(' ', array(\Google_Service_Calendar::CALENDAR));
$client = new \Google_Client();
$client->setAccessType('offline'); // default: offline
$client->setScopes($scope);
$client->setApplicationName(EVENT_GCAL_APPNAME);
$client->setClientId(isset($client_id) ? $client_id : EVENT_GCAL_CLIENTID_DEFAULT);
$client->setClientSecret(isset($client_secret) ? $client_secret : EVENT_GCAL_CLIENTSECRET_DEFAULT);
if (!empty($redirect_url)) {
    $client->setRedirectUri($redirect_url);
}

return $client;     

}

当错误触发时,我试试这个:

$refreshResult = $client->fetchAccessTokenWithRefreshToken($access_token['refresh_token']);

0 个答案:

没有答案
相关问题