Search code examples
phpgoogle-drive-apigoogle-oauth

Google Drive API push notification subscription 400 "parse error"


I'm trying to subscribe to get push notifications when a file changes in Google Drive. I've successfully implemented the Oauth2 procedure and receive an access token.

When I try to subscribe to the watch channel, I get a parse error which I assume means I have a mistake in the body of my request... but I can't figure it out.

Here are links to a couple of google resources that I'm working from:

https://developers.google.com/drive/v3/web/push

https://developers.google.com/drive/v3/reference/changes/watch

Here's my code to request the push notifications (php):

    //subscribe to new watch channel
    $ch = curl_init("https://www.googleapis.com/drive/v3/files/1fEDEciN1h_7MZJmNxmgUF-xxxxxxxxx/watch");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Authorization: Bearer '. $token["access_token"],
                    'Content-Type: application/json'
                ));
    $body_array = array(
                    "kind: api#channel", 
                    "id: ".generate_uuid(), // Your channel ID.
                    "resourceId: 1fEDEciN1h_7MZJmNxmgUF-xxxxxxxxx",
                    "resourceUri: https://docs.google.com/spreadsheets/d/1fEDEciN1h_7MZJmNxmgUF-xxxxxxxxx/",
                    "type: web_hook",
                    "address: http://exampmle.com/test/drivenotifications/receiver/index.php" // Your receiving URL.
                );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_array);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

And here's the error response:

"error": { "errors": [  {   "domain": "global",   "reason": "parseError",   "message": "Parse Error"  } ], "code": 400, "message": "Parse Error"}}

Any help/suggestions are much appreciated!

EDIT

I also tried an associative array per the comments:

$body_array = array(
                    "kind"=>"api#channel", 
                    "id"=>generate_uuid(), // Your channel ID.
                    "resourceId"=>"1fEDEciN1h_7MZJmNxmgUF-xxxxxxxxx",
                    "resourceUri"=>"https://docs.google.com/spreadsheets/d/1fEDEciN1h_7MZJmNxmgUF-xxxxxxxxx/",
                    "type"=>"web_hook",
                    "address"=>"http://example.com/test/drivenotifications/receiver/index.php" // Your receiving URL.
                );

But it resulted in the same error.


Solution

  • You need to json_encode the post data. Run json_encode on the $body _array before calling curl_setopt for CURLOPT_POSTFIELDS. Make sure that $body_array is an associative array before your run json_encode on it.