Chamath Sajeewa
Chamath Sajeewa

Reputation: 300

REST request to clear google calendar

I need to clear application's calendar using a REST request. Given below is my code

function clearCalender($token, $cal){
        $APIKEY = 'AIzaSyCK6TChY1yI0UY1HMym-LuSW5g7gIjooTEo';
        $request = 'https://www.googleapis.com/calendar/v3/calendars/'.$cal.'/clear?key='.$APIKEY;

        $session = curl_init($request);
        // Tell curl to use HTTP POST
        curl_setopt($session, CURLOPT_POST, true);
        // Tell curl not to return headers, but do return the response
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLINFO_HEADER_OUT, true);
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization:  Bearer ' . $token, ' X-JavaScript-User-Agent:  Resource Manager'));

        $response = curl_exec($session);
//        var_dump(curl_error( $session ));
//        curl_close($session);
        return $response;
    }

But response from Google say that

  1. That’s an error.Your client has issued a malformed or illegal request. That’s all we know.

I have already checked with Calendar API but could not find any error.

Upvotes: 0

Views: 148

Answers (2)

Chamath Sajeewa
Chamath Sajeewa

Reputation: 300

Finally got an answer. In API documentation, it is mentioned that "Do not supply a request body with this method". But we have to specifically say that there is no body for the request. To that I have added

curl_setopt($session, CURLOPT_POSTFIELDS, false);

and it works!!

Upvotes: 1

StigM
StigM

Reputation: 711

On the CURLOPT_HTTPHEADER line, you have a space before X-JavaScript-User-Agent. Replace that line with the following

curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization:  Bearer ' . $token, 'X-JavaScript-User-Agent:  Resource Manager'));

Upvotes: 1

Related Questions