Youtube Data API v3 - 错误请求 - 如何找到错误

时间:2014-05-25 10:08:19

标签: php youtube youtube-api

我想使用php检索有关Youtube视频的信息。

我有我的API密钥,我正在尝试这个:

$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=<vid-id>&key=<my-key>");
echo $res;

但是我收到了这个警告:

  

file_get_contents(https://www.googleapis.com/youtube/v3/videos?part=snippet&id=<vid-id&gt;&amp; key =&lt; my-key&gt;):无法打开流:HTTP请求失败! HTTP / 1.0 400错误请求

响应标题如下所示:

["http_response_header"]=>
  array(10) {
[0]=>
   string(24) "HTTP/1.0 400 Bad Request"
[1]=>
   string(45) "Content-Type: application/json; charset=UTF-8"
[2]=>
   string(35) "Date: Sun, 25 May 2014 09:50:12 GMT"
[3]=>
   string(38) "Expires: Sun, 25 May 2014 09:50:12 GMT"
[4]=>
   string(33) "Cache-Control: private, max-age=0"
[5]=>
   string(31) "X-Content-Type-Options: nosniff"
[6]=>
   string(27) "X-Frame-Options: SAMEORIGIN"
[7]=>
string(31) "X-XSS-Protection: 1; mode=block"
[8]=>
   string(11) "Server: GSE"
[9]=>
   string(28) "Alternate-Protocol: 443:quic"
}

现在我被卡住了。我不知道下一步该怎么做才能找出问题所在。我错过了什么吗?我做错了吗?我不知道。

任何提示?

修改

感谢hakre,我能够找到错误消息“accessnotconfigured”。

通过这个Stackoverflow Entry (second response),我想到了创建一个新项目。

然后我跳过了在推荐人中的输入,现在它有效了!

问题是什么,我不知道。我怀疑它与推荐人有关。

2 个答案:

答案 0 :(得分:0)

Youtube API具有HTTP响应正文的详细错误报告,它不在响应标头内。

因此,每当您从Youtube API(代码400到499或4xx)中获得HTTP错误状态代码时,您需要查看已提供的JSON。

要查看该JSON,您可以在测试中快速将URL输入浏览器。您将获得一些JSON,其中包含更好地描述您提出请求时的错误。

如果您想在PHP中以编程方式执行此操作,则需要通过$context参数(或HTTP-wrapper used by file_get_contents)告知setting the default context options它不应返回FALSE如果出现HTTP错误(全部为400-599个代码),但要返回响应正文,请参阅ingore_errros in HTTP context options

示例:

<?php

stream_context_set_default(['http' => ['ignore_errors' => true]]);
$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=<vid-id>&key=<my-key>");
echo $res;

这提供了以下JSON输出:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: part",
    "locationType": "parameter",
    "location": "part"
   }
  ],
  "code": 400,
  "message": "Required parameter: part"
 }
}

(你可能会有所不同,因为你已经获得了我没有的实际ID和KEY参数)

答案 1 :(得分:0)

我与picasaweb.google.com/data/feed/api有类似的问题。 file_get_contents()的问题在于,当存在HTTP错误时它会返回false,因此它不提供对响应正文的访问。 在浏览器中使用URL允许我在响应正文中查看错误消息并了解问题。

相关问题