1
votes

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.

1
in the $body_array, can you use a associative array in place of numeric key array - Jay Rajput
That was a good suggestion @JayRajput! I thought that might do it... but I tried the associative array and still got the parse error. (see edit above) Any other ideas? - Richard
Try checking the Google Drive Push Notifications Playground to verify the correct steps specially the code implementation (though their sample is python base code). You might also verify if you have done the required things before you can use the push notification like registering your domain and more. Hope this helps. - Mr.Rebot

1 Answers

1
votes

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.