我花了一整天时间搞清楚Google Calendar API。我终于设法在我的Google日历中插入一个事件,但现在我似乎无法使“list”命令工作。
以下代码有效:
<?php
$start = array(
"dateTime" => $date . "T" . $start_time . ":00",
"timeZone" => "Europe/Berlin"
);
$end = array(
"dateTime" => $date . "T" . $end_time . ":00",
"timeZone" => "Europe/Berlin"
);
$headerarray = array(
'Content-type: application/json',
'Authorization: Bearer ' . $access_token,
'X-JavaScript-User-Agent: Google APIs Explorer'
);
$post_data = array(
"start" => $start,
"end" => $end,
"summary" => $title,
"description" => $description,
"key" => $api_key
);
$post_data = json_encode($post_data);
$url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$response = json_decode($response);
?>
这段代码在我的日历中创建了一个新事件,所以我应该正确设置所有内容,对吗?
但是,此代码不工作:
<?php
$headerarray = array(
"Authorization: Bearer " . $access_token,
"X-JavaScript-User-Agent: Google APIs Explorer"
);
$url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events?key=' . $api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$response = json_decode($response);
?>
在这种情况下,我得到以下回复:Access Not Configured. Please use Google Developers Console to activate the API for your project.
但事实并非如此。我正确配置了访问权限,否则我无法将事件插入日历中,对吧?也许我没有正确使用cURL?
这是列表功能的参考:https://developers.google.com/google-apps/calendar/v3/reference/events/list
我在这里看不到什么?任何帮助都非常感谢。
答案 0 :(得分:1)
试试这个:
答案 1 :(得分:1)
我花了一整天来创建活动,我能够获取活动
这是我从google oauth获取数据的代码,我使用的是codeigniter框架。
<?php
// product_config
$config['google_id'] = '';
$config['google_secret'] = '';
// oauth file
function get_calendar_events(OAuth2_Token_Access $token){
try{
$max_results = 250;
$url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?showDeleted=false&$orderBy=email&maxResults='.$max_results.'&alt=json&v=3.0&oauth_token='.$token->access_token;
$google_events = json_decode(file_get_contents($url), true);
return $google_events;
}catch (Exception $e) {
// Exception
}
}
/*** Controller ***/
function myCalendar() {
try {
$events = array();
$provider_name = 'google';
$provider = $this->oauth2->provider($provider_name, array(
'id' => $this->config->item($provider_name.'_id', 'product_config'),
'secret' => $this->config->item($provider_name.'_secret', 'product_config'),
'scopes' => array('https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly')
));
if (!$this->input->get('code')){ // access authorizing
// By sending no options it'll come back here
$provider->authorize();
}else{
// Get the Token
$token = $provider->access($this->input->get('code'));
$events = $provider->get_calendar_events($token);
}catch (Exception $e) {
// Exception
}
}
?>
答案 2 :(得分:0)
public function create_calendar_event(OAuth2_Token_Access $token,$description, $leaveDate , $toLeaveDate,$title, $stime, $etime ){
try {
$title = $title;
//$start_time = '00:00'; $end_time = '23:59';
$start_time = $stime;
$end_time = $etime;
$timezone = 'Asia/Kolkata';
$start = array(
"dateTime" => $leaveDate . "T" . $start_time . ":00",
"timeZone" => $timezone
);
$end = array(
"dateTime" => $toLeaveDate . "T" . $end_time . ":00",
"timeZone" => $timezone
);
$headerarray = array(
'Content-type: application/json',
'Authorization: Bearer ' . $token->access_token,
'X-JavaScript-User-Agent: Google APIs Explorer'
);
$post_data = array(
"start" => $start,
"end" => $end,
"summary" => $title,
"description" => $description,
"key" => $this->api_key
);
$post_data = json_encode($post_data);
$calendar_id = 'primary';
$url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';
$result = $this->NewcurlRequest($url, $headerarray, $post_data);
}catch(Exception $e){
// Exception
}
}
public function NewcurlRequest($url,$headerarray,$post_data, $curl_call = true){
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
} catch (Exception $e) {
return $e;
}
}