谷歌日历API事件更新总是返回404"未找到"错误



我正在尝试使用完整日历更新谷歌日历事件。我使用完整日历成功地在谷歌日历中插入了一个事件。在更新时,我得到了404未找到的代码下面是错误的示例

"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}

以谷歌参考网站为例(https://developers.google.com/calendar/v3/reference/events/update#php)我正在尝试从完整日历更新谷歌日历事件。以下是我正在做的

//I am getting eventid from my db and all other values from the form
function edit_event_gcal(title,color,id) { 
$.ajax({
url: 'Calendar.php',
type: "POST",
data: {"title":title, "color":color, "task": "editCalendar", "eventid": id},
success: function(data) {
console.log("Data",data)
}
});
}

这是我的日历.php

require_once 'vendor/autoload.php'; //php library for google calendar
$sTask = (isset($_POST['task']) && !empty($_POST['task'])) ? $_POST['task'] : '';
$sTitle = (isset($_POST['title']) && !empty($_POST['title'])) ? $_POST['title'] : '';
$sColor = (isset($_POST['color']) && !empty($_POST['color'])) ? $_POST['color'] : '';
$sEventId = (isset($_POST['eventid']) && !empty($_POST['eventid'])) ? $_POST['eventid'] : '';
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$service = new Google_Service_Calendar($client);
$calendarId = 'mygooglecalendarid';
if($sTask == "editCalendar")
{
$event = $service->events->get('primary', $sEventId);
$event->setSummary('Appointment at Somewhere');
$updatedEvent = $service->events->update($calendarId, $event->getId(), $event);
//Print the updated date.
echo $updatedEvent->getUpdated();
}

有人能告诉我我做错了什么吗?我正在使用相同的配置进行插入。寻求积极回应。谢谢

$service->events->get需要日历id和事件id作为参数

重要的是要仔细检查两个参数是否正确

  • 如果'mygooglecalendarid'不是您的primary日历,那么您正试图从一个日历中获取事件并将其更新到另一个日历,这将导致404错误(错误请求(。

  • 如果eventid不正确,这也将导致404错误。后者可以通过使用事件的Try This API功能进行验证:获取

  • 检索正确事件id的可能方法是列出事件,或者检索calendar.google.com/calendar/r/eventedit/...url并通过解码构建事件id

相关内容

最新更新