我对 google api 的痛苦仍在继续……
我可以使用脚本来使用刷新令牌 - 自动刷新并保持我对日历的访问权限。我可以与日历交互,一切都很好。
我现在想继续使用 php 的 google api 客户端,我昨天在我的令牌有效时进行了这项工作,但现在它已经过期我无法弄清楚如何使用刷新令牌来获取新令牌并保留将来到期后进行身份验证。
我找到了许多参考资料并尝试了这些 - 这是我目前基于 simple.php 示例的测试脚本:
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("Downs Golfmanager");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('XXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXX');
$client->setRedirectUri('XXXXXX');
$client->setDeveloperKey('XXXXXX');
$client->refreshToken('X/XXXXX');
$client->setUseObjects(true);
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if($client->isAccessTokenExpired()) {
/
echo 'Access Token Expired'; // Debug
//$client->refreshToken($refreshToken);
}
if ($client->getAccessToken()) {
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-08-28T14:00:00.000+01:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-08-28T15:25:00.000+01:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('XXXXXX@group.calendar.google.com', $event);
//echo "<pre>" . print_r($createdEvent, true) . "</pre>";
echo $createdEvent->getId();
//test pulling events
$events = $cal->events->listEvents('XXXXXX@group.calendar.google.com');
while(true) {
foreach ($events->getItems() as $event) {
echo $event->getSummary();
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array(
'pageToken' => $pageToken,
'timeMin'=>'2013-08-26T00:00:00+01:00',
'timeMax'=>'2013-08-31T00:00:00+01:00'
);
$events = $cal->events->listEvents('XXXXXX@group.calendar.google.com', $optParams);
} else {
break;
}
}
//print "<h1>Events List</h1><pre>" . print_r($events, true) . "</pre>";
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
与我以前的帖子一样,我不清楚这里应该发生什么。我的令牌已过期 - 我有刷新令牌。每次访问时,我需要对我的代码进行哪些更改才能自动刷新授权。
编辑:错误代码是:The OAuth 2.0 access token has expired, and a refresh token is not available
我已经注释掉了上面的几行,我将它们包含在我试图解决的尝试中,只是为了避免进一步混淆
谢谢