【问题标题】:Google Calendar API event update always return 404 “not found” errorGoogle Calendar API 事件更新总是返回 404“未找到”错误
【发布时间】:2020-05-08 22:52:13
【问题描述】:

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

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

按照 Google 参考 (https://developers.google.com/calendar/v3/reference/events/update#php) 中的示例,我正在尝试从完整日历更新 google 日历事件。这是我在做什么

//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)
        }
    });
}

这是我的 Calendar.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%s\n", $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();
    }

谁能告诉我我做错了什么?我使用相同的配置进行插入。 寻找正面回应。谢谢

【问题讨论】:

    标签: php google-calendar-api


    【解决方案1】:

    $service->events->get 期望日历 ID 和事件 ID 作为参数

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

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

    • 如果 eventid 不正确,也会导致 404 错误。 后者可以使用Try This API feature for Events:get

    • 进行验证
    • 检索正确事件 ID 的可能方法是 listing events 或检索 calendar.google.com/calendar/r/eventedit/... url 并通过解码构建事件 ID。

    【讨论】:

    • 不,先生,这不是问题。我在写插入时提供了正确的日历 ID,它只是更新
    • 但是您定义 $event = $service->events->get('primary', $sEventId); 而不是 $event = $service->events->get('mygooglecalendarid', $sEventId); - 是 mygooglecalendarid您的主日历 ID?
    • 主要默认选择登录的日历,所以先生不用担心。我试图把我的 claendar id 也放在这里,但仍然是同样的错误
    • 您确定 eventid from my db 是此事件的有效日历 API ID 吗?使用Try this API for Events: get 进行检查
    • copied the event id from my google calendar 是什么意思?如果您在使用 Try this API 时遇到错误,则表示给定日历中具有给定 ID 的事件不存在。我建议你list你所有的事件和他们的ID来检索有效的ID。
    猜你喜欢
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多