Google Drive API推送通知订阅400"解析错误"

时间:2016-10-31 02:38:23

标签: php google-drive-api google-oauth2

我试图在Google云端硬盘中更改文件时订阅获取推送通知。我已成功实施Oauth2程序并收到访问令牌。

当我尝试订阅观看频道时,我得到一个解析错误,我认为这意味着我的请求正文中有错误...但我无法弄清楚。

以下是我工作的几个Google资源的链接:

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

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

这是我的代码,用于请求推送通知(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);

以下是错误回复:

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

非常感谢任何帮助/建议!

修改

我还根据评论尝试了一个关联数组:

$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.
                );

但它导致了同样的错误。

1 个答案:

答案 0 :(得分:1)

您需要json_encode发布数据。在为json_encode致电$body _array之前,在curl_setopt上运行CURLOPT_POSTFIELDS。在运行$body_array之前,请确保json_encode是一个关联数组。