1

I'm trying to perform a cURL request to google calendar api using their guide, that says:

POST https://www.googleapis.com/calendar/v3/calendars/{name_of_my_calendar}/events?sendNotifications=true&pp=1&key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  OAuth 1/SuypHO0rNsURWvMXQ559Mfm9Vbd4zWvVQ8UIR76nlJ0
X-JavaScript-User-Agent:  Google APIs Explorer

{
 "start": {
  "dateTime": "2012-06-03T10:00:00.000-07:00"
 },
 "end": {
  "dateTime": "2012-06-03T10:20:00.000-07:00"
 },
 "summary": "my_summary",
 "description": "my_description"
}

How am I supposed to do that in php? I wonder what parameters I should send and what constants I should use. I'm currently doing:

        $url = "https://www.googleapis.com/calendar/v3/calendars/".urlencode('{name_of_my_calendar}')."/events?sendNotifications=true&pp=1&key={my_api_key}";  

        $post_data = array(  
            "start" => array("dateTime" => "2012-06-01T10:00:00.000-07:00"),  
            "end" => array("dateTime" => "2012-06-01T10:40:00.000-07:00"),  
            "summary" => "my_summary",
            "description" => "my_description"
        );

        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($ch, CURLOPT_POST, 1);  
        // adding the post variables to the request  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  

        $output = curl_exec($ch);  

        curl_close($ch);  

but the response is:

{
    error: {
        errors: [
        {
            domain: "global",
            reason: "required",
            message: "Login Required",
            locationType: "header",
            location: "Authorization"
        }
        ],
        code: 401,
        message: "Login Required"
    }
}

How should I format my parameters?

Gustav
  • 1,361
  • 1
  • 12
  • 24
  • Did you read https://developers.google.com/google-apps/calendar/auth? – Daan Apr 19 '12 at 08:44
  • Yes, my code works on an example where I just parse events from the same calendar. – Gustav Apr 19 '12 at 08:46
  • Hmm. Not sure, I've never worked with this API before. You may want to give the official PHP API package a try: http://code.google.com/p/google-api-php-client/ Hope this helps a bit! – Daan Apr 19 '12 at 17:45

1 Answers1

2

I noticed this question is asked quite some time ago, however after figuring out the post-parameter problem after some time I thought it might be useful to others to answer it. First within '$post_data', I switched the 'start' and 'end':

$post_data = array(
    "end" => array("dateTime" => "2012-06-01T10:40:00.000-07:00"),
    "start" => array("dateTime" => "2012-06-01T10:00:00.000-07:00"),  
    "summary" => "my_summary",
    "description" => "my_description"
);

Secondly, I figured Google Calendar API expected the data to be json, so in curl_setopt:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

This worked perfectly for me, hope it's useful to someone else as well!

Joep
  • 21
  • 4
  • For me is not working. i tryed that code, or i'm doing wrong key, but i don't think so. is the one under : "Key for server apps (with IP locking) " i allowed all IP for now. authorization required even for me. Can i have access to your complete example please? – AndreaBogazzi Oct 25 '13 at 10:57