dongqian7545 2018-01-31 14:17
浏览 147
已采纳

Google日历API会创建群组日历活动

My goal is to insert events to a group calendar VIA PHP

  1. I have created an app on api console
  2. I have activated calendar API and created API KEY
  3. I have created a service account as described here

Here is my code:

class Googl_Cal{
    public function __construct(){
        $this->client = $this->get_client();
    }

    public function get_client()
    {

       putenv('GOOGLE_APPLICATION_CREDENTIALS=/' . dirname( __FILE__ ) . '/gcal_service_account.json');
       $client = new Google_Client();
       $client->useApplicationDefaultCredentials();
       $scopes = [ Google_Service_Calendar::CALENDAR ];
       $client->setScopes($scopes);

       return $client;
    }

    public function create_event(){

       $service = new Google_Service_Calendar( $this->client );

       $calendarList = $service->calendarList->listCalendarList();

       $calendarId = '';

       $event = new Google_Service_Calendar_Event(
            array(
                'summary' => 'Google I/O 2015',
                'location' => '800 Howard St., San Francisco, CA 94103',
                'description' => 'A chance to hear more about Google\'s developer products.',
                'start' => array(
                    'dateTime' => '2018-01-31T09:00:00-07:00',
                    'timeZone' => 'Asia/Jerusalem',
                ),
                'end' => array(
                    'dateTime' => '2018-01-31T17:00:00-07:00',
                    'timeZone' => 'Asia/Jerusalem',
                ),
                'recurrence' => array(
                    'RRULE:FREQ=DAILY;COUNT=2'
                ),
                'reminders' => array(
                    'useDefault' => FALSE,
                    'overrides' => array(
                        array('method' => 'email', 'minutes' => 24 * 60),
                        array('method' => 'popup', 'minutes' => 10),
                    ),
                ),
            )
        );

       $calendarId = 'primary';

       $event = $service->events->insert($calendarId, $event);

       print_r($event);
       die();
    }
}

After calling create_event(); Im getting an empty list on $calendarList

  • 写回答

1条回答 默认 最新

  • doujuanju3076 2018-01-31 14:22
    关注

    Remember that a service account is not you you will be inserting into the service accounts calendar.

    I suspect part of your issue is that you are not inserting it into a calendar and are probably getting an error.

    $calendarId = '';

    I sugest you do

    $calendarId = 'primary';

    This will insert into the service accounts primary calendar.

    Assuming you want it writing to one of your personal calendars. Take the service account email address and share the calendar with it in the Web google calendar app. It will then be able to write to it. Tip grab the calendar id out of the web site while you are at it as calendarlist.list isnt going to return it until you add it, this is an annoying feature in service accounts.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀