问题描述
未捕获的Google_Service_Exception:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“未找到”,“消息”:“未找到”}],“代码”:404 ,“消息”:“未找到”}}
下面是我的代码,我已经在其中拥有auth2访问令牌
require "../_lib/libraries/grp/googleCalendar/vendor/autoload.PHP";
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAuthConfig('../_lib/libraries/grp/googleCalendar/credentials.json');
$client->setAccesstype('offline');
$client->setPrompt('select_account consent');
//$_SESSION['access_token'] = '';
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
print_r($_SESSION['access_token']);
// Exchange authorization code for an access token.
//$accesstoken = $client->fetchAccesstokenWithAuthCode($_SESSION['access_token']);
$client->setAccesstoken($_SESSION['access_token']);
// If there is no prevIoUs token or it's expired.
if ($client->isAccesstokenExpired()) {
// Refresh the token if possible,else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccesstokenWithRefreshToken($client->getRefreshToken());
} else {
// Exchange authorization code for an access token.
$accesstoken = $_SESSION['access_token'];
$client->setAccesstoken($accesstoken);
}
}
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,'orderBy' => 'startTime','singleEvents' => true,'timeMin' => date('c'),);
//here the error occurs
$calendar = $service->calendars->get('calendarid');
echo $calendar->getSummary();
$results = $service->events->listEvents($calendarId,$optParams);
$events = $results->getItems();
if (empty($events)) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($events as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n",$event->getSummary(),$start);
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'].'/scriptcase9/app/project_gestao/oauth2callback/index.PHP';
header('Location: ' . filter_var($redirect_uri,FILTER_SANITIZE_URL));
}
这是获得访问令牌auth02的一部分,但现在我需要calendarid,并且错误如上所述返回。
如前所述,访问令牌已经可以正常显示事件,但是我需要使用日历ID。
解决方法
您的代码包含该行
N is N1 + 1
此处的 $calendar = $service->calendars->get('calendarid');
应该用真实ID(您可以访问的日历的ID)或变量代替。
鉴于在您定义'calendarid'
之前,有必要将代码修改为
$calendarId = 'primary';