【问题标题】:Google Drive API push notification subscription 400 "parse error"Google Drive API 推送通知订阅 400“解析错误”
【发布时间】:2016-10-31 02:38:23
【问题描述】:

我正在尝试订阅以在 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"}}

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

编辑

我还尝试了每个 cmets 的关联数组:

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

但它导致了同样的错误。

【问题讨论】:

  • 在$body_array中,能不能用关联数组代替数字键数组
  • 这是一个很好的建议@JayRajput!我认为这可能会做到......但我尝试了关联数组并仍然得到解析错误。 (见上面的编辑)还有其他想法吗?
  • 尝试检查Google Drive Push Notifications Playground 以验证正确的步骤,特别是代码实现(尽管它们的sample 是python 基础代码)。您还可以在使用push notification 之前验证您是否已完成所需的事情,例如注册您的域等等。希望这会有所帮助。

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


【解决方案1】:

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

【讨论】:

  • 就是这样,杰伊!我自己也得出了同样的结论并回来发布答案......它总是你花了 3 天时间试图找到的简单事情! ;) 非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 2017-05-13
  • 1970-01-01
  • 2016-05-09
相关资源
最近更新 更多