【发布时间】:2014-07-26 17:51:10
【问题描述】:
我花了一整天时间研究 Google Calendar API。我终于设法在我的 Google 日历中插入了一个活动,但现在我似乎无法让“列表”命令正常工作。
以下代码有效:
<?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
我在这里没有看到什么?非常感谢任何帮助。
【问题讨论】:
-
好吧,没关系!现在可以了。我不知道为什么。我不知道为什么它以前不起作用。我没有做任何改变,它突然起作用了。 -.-
-
为什么在你的第一个例子中使用
curlopt_httpget和curlopt_post?
标签: php curl calendar google-api google-calendar-api