Hello everyone,
I'm using PHP + curl to do GET querys against Google Calendar API and everything works as intended, I get a list of events depending of what parameters I use.
The problem is when I'm trying to do create a POST to insert a new event on the calendar, curl always returns error and no event is created in the Calendar.
I have been tried a lot of times with examples I found browsing the web without luck. Basically I tried these 2 ways:
- Creating the JSON code with the parameters I want to use to create the event.
- Writing in the URL all the parameters I have to use.
If I'm doing something wrong please tell me because I have little experience with Google Calendar.
The first code example I tried is:
// Create the JSON code for the event I want insert
function createPostArgsJSON($date, $startTime, $endTime, $title, $description)
{
$arg_list = func_get_args();
foreach($arg_list as $key => $arg){
$arg_list[$key] = urlencode($arg);
}
$postargs = <<<JSON
{
"start": {
"dateTime": "{$date}T{$startTime}:00Z"
},
"end": {
"dateTime": "{$date}T{$endTime}:00Z"
},
"summary": "$title",
"description": "$description"
}
JSON;
return $postargs;
}
$json_postArgs = createPostArgsJSON("2012-07-07", "01:30", "02:30", "aaa", "bbb");
/*
$json_postArgs will receive:
{
"start": {
"dateTime": "2012-07-07T03:30:00.000Z"
},
"end": {
"dateTime": "2012-07-07T08:30:00.000Z"
},
"summary": "aaa",
"description": "bbb"
}
*/
function sendPostRequest($postArgs, $aToken, $calendar_id)
{
global $api_key;
$request = "
https://www.googleapis.com/calendar/v3/calendars/".$calendar_id."/events?key=".$api_key;
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $request);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $aToken));
curl_setopt($session, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($session);
// var_dump(curl_getinfo($session));
curl_close($session);
return $response;
}
/*
Returns if I execute it with:
var_dump(sendPostRequest($json_postArgs, $accessToken, $book_calendar_id));
/*
// Output:
string 'HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=UTF-8
Date: Thu, 05 Jul 2012 17:05:32 GMT
Expires: Thu, 05 Jul 2012 17:05:32 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "accessNotConfigured",
"message": "Access Not Configured"
}
],
"code": 403,
"message": "Access Not Con'... (length=526)
*/
$accessToken, $api_key, $book_calendar_id have valid values I have been using for the GET queries.
If I uncomment the "var_dump(curl_getinfo($session));" line in the sendPostRequest function I get:
array
'url' => string 'https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXXXXXXX
/events?key=YYYYYYYYYYY' (length=152)
'content_type' => string 'application/json; charset=UTF-8' (length=31)
'http_code' => int 403
'header_size' => int 321
'request_size' => int 470
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 0.281
'namelookup_time' => float 0
'connect_time' => float 0.062
'pretransfer_time' => float 0.203
'size_upload' => float 148
'size_download' => float 205
'speed_download' => float 729
'speed_upload' => float 526
'download_content_length' => float -1
'upload_content_length' => float 148
'starttransfer_time' => float 0.281
'redirect_time' => float 0
'certinfo' =>
array
empty
'redirect_url' => string '' (length=0)
A lot of unitialized values as you can see.
*/
The second code example I tried is:
function book_new()
{
global $api_key;
global $accessToken;
global $book_calendar_id;
$bookNewQuery = "
https://www.googleapis.com/calendar/v3/calendars/$book_calendar_id/events?summary=aaaa&description=bbbbb&start.dateTime=2012-07-06T01:30:00.000Z&end.dateTime=2012-07-06T02:30:00.000ZsendNotifications=false&key=$api_key";
return send_post($bookNewQuery);
}
function send_post($query)
{
global $api_key;
global $accessToken;
$curl = curl_init($query);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$curlheader[0] = "Authorization: Bearer ".$accessToken;
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
////curl_setopt($session, CURLOPT_POSTFIELDS, $postArgs); // No works when I put here all the parameters
$response = curl_exec($curl);
//var_dump(curl_getinfo($curl));
curl_close($curl);
return $response;
}
Calling "var_dump(book_new());" I get this error;
string 'HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 925
Date: Thu, 05 Jul 2012 17:08:34 GMT
Server: GFE/2.0
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:'... (length=1068)
If I uncomment the "var_dump(curl_getinfo($curl));" line in the send_post function I get:
array
'url' => string 'https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXXXXXXX/events?summary=aaaa&description=bbbbb&start.dateTime=2012-07-06T01:30:00.000Z&end.dateTime=2012-07-06T02:30:00.000ZsendNotifications=false&key=YYYYYYYYYYYYY' (length=284)
'content_type' => string 'text/html; charset=UTF-8' (length=24)
'http_code' => int 400
'header_size' => int 143
'request_size' => int 490
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 0.25
'namelookup_time' => float 0
'connect_time' => float 0.063
'pretransfer_time' => float 0.188
'size_upload' => float 0
'size_download' => float 925
'speed_download' => float 3700
'speed_upload' => float 0
'download_content_length' => float 925
'upload_content_length' => float -1
'starttransfer_time' => float 0.25
'redirect_time' => float 0
'certinfo' =>
array
empty
'redirect_url' => string '' (length=0)
I'm completly lost at this point, I can't replicate my successful inserts from the OAuth 2.0 Playground or from the query builder at
https://developers.google.com/google-apps/calendar/v3/reference/events/insert and I can't use the Google API client for PHP.
Please, can someone explain me what I'm doing wrong in these examples? I strongly believe that must be a very insignificant detail I forgot but after dozens of examples I'm lost &
frustrated.