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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办