Google Calendar API v3 - Google_AuthException - 无法json解码令牌问题

时间:2013-10-02 04:46:15

标签: php google-calendar-api

我搜索了这个群组,以及其他网站,以及其他网站,但无法找到解决方法。错误输出如下。我当然知道setAccessToken不应该是NULL。任何指导都会很棒。 Google Calendar v3 API文档并不是那么好......事实上,这些示例适用于较旧的API版本。

PHP致命错误:未捕获的异常'Google_AuthException',并在google-api-php-client / src / auth / Google_OAuth2.php中显示消息'无法json解码令牌':162 堆栈跟踪:

0 google-api-php-client / src / Google_Client.php(170):Google_OAuth2-> setAccessToken(NULL)

1 Cal.php(16):Google_Client-> setAccessToken(true)

2 {main}   在第162行的google-api-php-client / src / auth / Google_OAuth2.php中抛出

以下是我的应用的代码:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$service = new Google_CalendarService($client);
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
} else {
  $client->setAccessToken($client->authenticate($_GET['code']));
  $_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) { 
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-10-05T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-10-05T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('my@email.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
} else {
echo "failed hard";
}
?>

ClientID,Key等保存在我的google-api-php-client / src / config.php文件中

1 个答案:

答案 0 :(得分:0)

我暂时没有查看api文档,但我使用类似下面的内容来配置客户端。也许它会有所帮助。

$certFile = file_get_contents('/path/to/cert.p12');

$client = new Google_Client();
$client->setApplicationName('My App');
$client->setClientId($clientId);
$client->setScopes([
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
]);

if ($token = $_SESSION['google.calendar.token']) {
    $client->setAccessToken($token);
}

$credentials = new Google_AssertionCredentials($service_email, $client->getScopes(), $certFile);
$client->setAssertionCredentials($credentials);
$service = new Google_CalendarService($client);