【问题标题】:Google Calendar Events谷歌日历活动
【发布时间】:2015-06-20 17:04:41
【问题描述】:

我在向 Google 日历添加活动时遇到了一些问题。我可以列出事件,但是当我尝试添加事件时,我收到 403 禁止错误。我可以将事件添加到主事件,但是当我尝试另一个事件时,在本例中是我已经提到的事件,我遇到了 403 禁止错误。这是我的代码:

require_once 'vendor/autoload.php';
define('APPLICATION_NAME', 'Calendar');
define('CREDENTIALS_PATH', 'credentials/calendar.json');
define('SECRET_PATH', 'secret.json');
define('SCOPES', implode(' ', array(Google_Service_Calendar::CALENDAR)));

function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(SECRET_PATH);
    $client->setAccessType('offline');

    $credentials_path = CREDENTIALS_PATH;

    if (file_exists($credentials_path)) {
        $access_token = file_get_contents($credentials_path);
    } else {
        // $auth_url = $client->createAuthUrl();
        // printf("Open the following link in your browser:\n%s\n", $auth_url);

        // print 'Enter verification code: ';

        // $auth_code = trim(fgets(STDIN));

        // $access_token = $client->authenticate($auth_code);

        // if (!file_exists(dirname($credentials_path))) {
            // mkdir(dirname($credentials_path), 0700, true);
        // }

        // file_put_contents($credentials_path, $access_token);
        // printf("Credentials saved to %s\n", $credentials_path);
}

   $client->setAccessToken($access_token);

   if ($client->isAccessTokenExpired()) {
       $client->refreshToken($client->getRefreshToken());
       file_put_contents($credentials_path, $client->getAccessToken());
   }

   return $client; 

 }



   $client = getClient();

   $service = new Google_Service_Calendar($client);


   $event = new Google_Service_Calendar_Event();
   $event->setSummary('Appointment');
   $event->setLocation('Somewhere');
   $start = new Google_Service_Calendar_EventDateTime();
   $start->setDateTime('2015-04-14T10:00:00.000-07:00');
   $event->setStart($start);
   $end = new Google_Service_Calendar_EventDateTime();
   $end->setDateTime('2015-04-15T10:25:00.000-07:00');
   $event->setEnd($end);
   $attendee1 = new Google_Service_Calendar_EventAttendee();
   $attendee1->setEmail('leah@leahdawn.com');

   $attendees = array($attendee1);
   $event->attendees = $attendees;
   $createdEvent = $service->events->insert('leah@leahdawn.com', $event);

   echo $createdEvent->getId();

具体来说,错误是'Error calling POST https://www.googleapis.com/calendar/v3/calendars/leah%40leahdawn.com/events: (403) Forbidden.

任何帮助将不胜感激!

【问题讨论】:

    标签: php google-calendar-api gdata google-api-php-client


    【解决方案1】:

    Events: insert 的第一个参数是一个日历 ID,而'leah@leahdawn.com' 可以是一个有效的日历 ID。如果所有者与您共享此日历,您甚至可以看到它。这并不意味着该日历的所有者已授予您向该日历添加事件的权限。

    我建议你运行CalendarList: get

     $calendarListEntry = $service->calendarList->get('calendarId');
     echo $calendarListEntry->getSummary();
    

    如果您有权访问,这将为您提供有关此日历的信息。

      {
    
     "kind": "calendar#calendarListEntry",
     "etag": "\"1412244000929000\"",
     "id": "xxxxx@gmail.com",
     "summary": "xxxxx@gmail.com",
     "timeZone": "Europe/Copenhagen",
     "colorId": "15",
     "backgroundColor": "#9fc6e7",
     "foregroundColor": "#000000",
     "selected": true,
     "accessRole": "reader",
     "defaultReminders": [
     ]
    }
    

    这里要查找的是"accessRole": "reader",,如果您无权执行更多操作,请阅读日历,您将无法添加事件。

    更新:CalendarList:列表

    尝试去底部运行的Calendar list page 试试我。它还将向您显示您可以访问哪些日历。

    【讨论】:

    • 现在它正在抛出异常“调用 GET www,googleapis.com/calendar/v3/users/me/calendarList leah%40leahdawn.com 时出错:(404) 未找到。它可以很好地显示 listEvents 方法中的事件,知道为什么这不起作用吗?
    • 检查 CalendarList: list 确保您有权访问该日历。至少验证日历将返回给您正确的日历 ID,您可能错过了拼写或其他内容。如果 Calendarlist.get 返回 Not found 我真的怀疑您无权访问该日历。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多