Google Calendar API事件更新始终返回404“未找到"错误 [英] Google Calendar API event update always return 404 “not found” error

查看:43
本文介绍了Google Calendar API事件更新始终返回404“未找到"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to update a google calendar event using full calendar. I successfully inserted an event in google calendar using full calendar. While updating I am getting 404 not found code below is the sample of error

 "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日历事件.这是我在做什么

Following the example at Google Reference (https://developers.google.com/calendar/v3/reference/events/update#php) I am trying to update google calendar event from Full Calendar. Here what I am doing

//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

Here is my 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();
    }

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

Can anyone tell me what I am doing wrong? I am using same configuration for inserting. Looking for Positive response. Thank you

推荐答案

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

仔细检查两个参数的正确性很重要

$service->events->get expects the calendar id and event id as parameters

It is important to double-check that both parameters are correct

  • 如果'mygooglecalendarid'不是您的 primary 日历,则您正在尝试从一个日历获取事件并将其更新为另一个日历-此会给您404错误(错误请求).

  • If 'mygooglecalendarid' is not your primary calendar, then you are trying to get the event from one calendar and update it into a different one - this will give you a 404 error (bad request).

如果eventid不正确,这还将导致404错误.可以使用针对事件:获取此API功能:get

If the eventid is not correct, this will also lead to a 404 error. The latter can be verified by using the Try This API feature for Events:get

这篇关于Google Calendar API事件更新始终返回404“未找到"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆