1

我已经搜索了该组以及其他组以及其他网站,但找不到解决方案。错误输出如下。我绝对知道 setAccessToken 不应该为 NULL。任何指导在这里都会很棒。Google Calendar v3 API 文档不是很好……事实上,这些示例适用于较旧的 API 版本。

PHP 致命错误:在 google-api-php-client/src/auth/Google_OAuth2.php:162 中未捕获异常 'Google_AuthException' 并带有消息 'Could not json decode the token' 堆栈跟踪:

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 文件中

4

1 回答 1

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);
于 2013-10-02T05:01:43.047 回答