douye4254 2018-08-10 08:34
浏览 87

试图让参与者设置谷歌日历API / PHP

I´m trying to create an event and that part works, but it will not for some reason send out the invitations / attendees in the calendar system with google API using PHP. It does create the event but that also it. So I hope someone can help me figuring out what I´m doing wrong.

myfile.php // Event details

parameters = {  title: $("#event-title").val(), 

                event_time: {

                    start_time: $("#event-type").val() == 'FIXED-TIME' ? $("#event-start-time").val().replace(' ', 'T') + ':00' : null,

                    end_time: $("#event-type").val() == 'FIXED-TIME' ? $("#event-end-time").val().replace(' ', 'T') + ':00' : null,

                    event_date: $("#event-type").val() == 'ALL-DAY' ? $("#event-date").val() : null

                },
                'attendees': [
{'email': 'test@dds-slagelse.dk'},
],

                all_day: $("#event-type").val() == 'ALL-DAY' ? 1 : 0,

            };




$("#create-event").attr('disabled', 'disabled');

$.ajax({

    type: 'POST',

    url: 'ajax.php',

    data: { event_details: parameters },

    dataType: 'json',

    success: function(response) {

        $("#create-event").removeAttr('disabled');

        alert('Event created with ID : ' + response.event_id);

    },

    error: function(response) {

        $("#create-event").removeAttr('disabled');

        alert(response.responseJSON.message);

    }

});

ajax.php file

session_start();
header('Content-type: application/json');

require_once('google-calendar-api.php');
error_log($_SESSION['access_token']);
try {
// Get event details
$event = $_POST['event_details'];
error_log(__LINE__);
$capi = new GoogleCalendarApi();
error_log(__LINE__);
// Get user calendar timezone
$user_timezone = $capi->GetUserCalendarTimezone($_SESSION['access_token']);
error_log(__LINE__);
// Create event on primary calendar
error_log($event['attendees0']);
$event_id = $capi->CreateCalendarEvent('primary', $event['title'], $event['all_day'], $event['event_time'], $event['attendees'], $user_timezone, $_SESSION['access_token']);
error_log(__LINE__);
echo json_encode([ 'event_id' => $event_id ]);
}
catch(Exception $e) {
error_log($e->getMessage());
header('Bad Request', true, 400);
echo json_encode(array( 'error' => 1, 'message' => $e->getMessage() ));
}

google-calendar.api.php

class GoogleCalendarApi

{

public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {  

    $url = 'https://accounts.google.com/o/oauth2/token';            


    $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';

    $ch = curl_init();      

    curl_setopt($ch, CURLOPT_URL, $url);        

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        

    curl_setopt($ch, CURLOPT_POST, 1);      

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    

    $data = json_decode(curl_exec($ch), true);

    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      

    if($http_code != 200) 

        throw new Exception('Error : Failed to receieve access token');



    return $data;

}



public function GetUserCalendarTimezone($access_token) {

    $url_settings = 'https://www.googleapis.com/calendar/v3/users/me/settings/timezone';

    $ch = curl_init();      

    curl_setopt($ch, CURLOPT_URL, $url_settings);       

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));   

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

    $data = json_decode(curl_exec($ch), true); //echo '<pre>';print_r($data);echo '</pre>';

    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      

    if($http_code != 200) 
        throw new Exception($http_code);



    return $data['value'];

}



public function GetCalendarsList($access_token) {

    $url_parameters = array();



    $url_parameters['fields'] = 'items(id,summary,timeZone)';

    $url_parameters['minAccessRole'] = 'owner';



    $url_calendars = 'https://www.googleapis.com/calendar/v3/users/me/calendarList?'. http_build_query($url_parameters);



    $ch = curl_init();      

    curl_setopt($ch, CURLOPT_URL, $url_calendars);      

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));   

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

    $data = json_decode(curl_exec($ch), true); //echo '<pre>';print_r($data);echo '</pre>';

    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      

    if($http_code != 200) 

        throw new Exception('Error : Failed to get calendars list');



    return $data['items'];

}


//'primary', $event['title'], $event['all_day'], $event['event_time'], $event['attendees'], $user_timezone, $_SESSION['access_token']
public function CreateCalendarEvent($calendar_id, $summary, $all_day, $event_time, $event_attendees, $event_timezone, $access_token) {

    $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';



    $curlPost = array('summary' => $summary);

    if($all_day == 1) {

        $curlPost['start'] = array('date' => $event_time['event_date']);

        $curlPost['end'] = array('date' => $event_time['event_date']);

    }

    else {

        $curlPost['start'] = array('dateTime' => $event_time['start_time'], 'timeZone' => $event_timezone);

        $curlPost['end'] = array('dateTime' => $event_time['end_time'], 'timeZone' => $event_timezone);

    }

    $ch = curl_init();      

    curl_setopt($ch, CURLOPT_URL, $url_events);     

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        

    curl_setopt($ch, CURLOPT_POST, 1);      

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json')); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));   

    $data = json_decode(curl_exec($ch), true);

    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      

    if($http_code != 200) 

        throw new Exception($http_code);



    return $data['id'];

}

}

I hope this make sense as I have tried so many attempts that I have lost track of it.

The Form Data that is send (after what i can figure out from google chrome is the following:

event_details[title]: Privat
event_details[event_time][start_time]: 2018-08-11T13:00:00
event_details[event_time][end_time]: 2018-08-11T15:30:00
event_details[event_time][event_date]: 
event_details[attendees][0][email]: test@dds-slagelse.dk
event_details[all_day]: 0
  • 写回答

1条回答 默认 最新

  • doudou6719 2018-08-11 04:14
    关注

    ADyson, thanks for your help / hint. It was the missing curl that was the solution.

    $curlPost['attendees'] = $event_attendees;

    I hope it wont be to much of a bother, but it seems that it does not send out the invites for the events as hoped, even thought it does add the "guests" to the event.

    The starting point for this solution is found here: http://usefulangle.com/post/29/google-calendar-api-create-event-php and if someone can please helt me to get * "sendNotifications"=>true * inserted or set correctly I would be very thankful. (would like to see how that parameter / function is set).

    Best Regards Jess

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值