我正在尝试使用PHP中的Google Calendar API来访问一个中央帐户 . 其想法是其他用户可以向此中央帐户添加事件(他们不必登录Google帐户),因此我希望永久访问该帐户 .
首先,我做了Quickstart tutorial on Google's webpage . 我登录了我的帐户,一切都工作了一段时间 . 之后,我的访问令牌已过期,现在, $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
抛出LogicException:刷新令牌必须传入或设置为setAccessToken的一部分 . 我发现 $client->getRefreshToken()
返回null .
在这里,您可以查看我的整个代码以获取Google客户端:
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(implode(' ', array(Google_Service_Calendar::CALENDAR) ) );
$client->setAuthConfig(__DIR__ . '/../config/client_secret.json');
$client->setAccessType('offline');
$credentialsPath = __DIR__ . '/../config/calendar-php-quickstart.json';
$accessToken = json_decode(file_get_contents($credentialsPath), true);
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
知道我做错了什么吗?
3 回答
尝试使用DalmTo关于Calendar API and service accounts的教程 .
这是她的指南中的刷新令牌实现:
您可以测试以下代码:
我将分享我最终开始工作的代码 . 我正在使用前端Web应用程序获取初始授权,然后将该代码传递给php Web服务以存储在dbase中供以后使用 .
希望能帮助别人 . 文档很难分类 .