Google Calendar API事件更新始终返回404“未找到”错误

问题描述 投票:0回答:1

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

 "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个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.