尝试让参与者在Google Calendar API / PHP中设置

时间:2018-08-10 08:34:58

标签: php ajax google-api calendar google-calendar-api

我正在尝试创建一个活动并且该部分有效,但是由于某种原因,它将不会使用PHP使用Google API在日历系统中发送邀请/与会者。它确实创建了事件,但也创建了事件。因此,我希望有人可以帮助我弄清楚我做错了什么。

myfile.php         //活动详细信息

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文件

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'];

}

}

我希望这是有道理的,因为我尝试了很多次尝试,以至于迷失了方向。

要发送的表单数据(在我可以从Google chrome中找出以下信息之后:

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 个答案:

答案 0 :(得分:0)

ADyson,感谢您的帮助/提示。解决的方法是缺少卷发。

$ curlPost ['attendees'] = $ event_attendees;

我希望这不会造成太大的麻烦,但是似乎它并没有像希望的那样发出活动邀请,甚至认为它确实为活动增加了“客人”。

此解决方案的起点位于:http://usefulangle.com/post/29/google-calendar-api-create-event-php,如果有人可以帮助我获得 *“ sendNotifications” => true * 的正确插入或设置,我将非常感激不尽(想了解如何设置该参数/功能)。

最佳问候 杰西