Google Calendar API(v3)-PHP可以写但不能读吗? [英] Google Calendar API (v3) - PHP can write but not read?
问题描述
我花了一整天的时间弄清楚Google Calendar API。我终于设法在我的Google日历中插入了一个事件,但是现在我似乎无法使列表命令正常工作。
I spent all day figuring out the Google Calendar API. I finally managed to insert an event in my Google Calendar but now I cannot seem to get the "list" command working.
以下代码可行 >:
<?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);
?>
这段代码在我的日历中创建了一个新事件,因此我应该正确设置所有内容,对吗?
This piece of code creates a new event in my calendar, so I should have everything set up correctly, right?
但是此代码无效起作用:
<?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);
?>
在这种情况下,我得到以下响应:未配置访问。请使用Google Developers Console激活您项目的API。
In this case I get the following response: Access Not Configured. Please use Google Developers Console to activate the API for your project.
但是事实并非如此。我已正确配置访问权限,否则我将无法在日历中插入事件,对吗?也许我没有正确使用cURL?
But that is not the case. I configured the access correctly, or else I would not be able to insert events into the calendar, right? Maybe I am not using cURL right?
这是列表函数的参考: https://developers.google.com/google-apps/calendar/v3/reference/events/list
This is the reference for the list function: https://developers.google.com/google-apps/calendar/v3/reference/events/list
我在这里没看到什么?非常感谢您的帮助。
What am I not seeing here? Any help is highly appreciated.
推荐答案
我花了一整天的时间来创建活动,并且能够获取活动
I spent my whole day to create the event , I'm able to fetch the event
这是我的代码,用于从Google oauth提取数据,并且我正在使用codeigniter框架。
here is my code to fetch data from google oauth and I am using the codeigniter framework.
<?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
}
}
?>
这篇关于Google Calendar API(v3)-PHP可以写但不能读吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!