douchun1948 2015-04-14 22:16
浏览 37
已采纳

Google日历活动

I am having some trouble adding events to Google Calendar. I can list events but when I try to add an event I am getting a 403 Forbidden Error. I am able to add events to primary, but when I try another one, in this case the one I've mnetion, I run into the 403 Forbidden Error. Here is the code I have:

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:
%s
", $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
", $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();

Specifically, the error is 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/leah%40leahdawn.com/events: (403) Forbidden.

Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • doudang1052 2015-04-15 07:42
    关注

    The first parameter of Events: insert is a calendar id, while 'leah@leahdawn.com' could be a valid calendar id. You may even be able to see this calendar if the owner has shared it with you. This doesn't mean that the owner of said calendar has given you permission to add events to there calendar.

    I suggest you run a CalendarList: get

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

    This will give you back the information about this calendar if you have access.

      {
    
     "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": [
     ]
    }
    

    The thing to look for here is "accessRole": "reader", if you don't have access to do more then read the calendar you wont be allowed to add events.

    Update: CalendarList : List

    try going to the Calendar list page at the bottom run try me. It will also show you what calendars you have access to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?