- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想使用 php 根据 id 检索有关 Youtube 视频的信息。
我有我的 API key ,我正在尝试这样做:
$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>&key=<my-key>): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
响应头看起来像这样:
["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)我突然想到创建一个新项目。
然后我就跳过了输入推荐人的步骤,现在它可以工作了!
问题是什么,我不知道。我怀疑它与推荐人有关。
最佳答案
Youtube API 在 HTTP 响应主体中有详细的错误报告,它不在响应 header 中。
因此,每当您从 Youtube API 返回 HTTP 错误状态代码(代码 400 到 499 或简称 4xx)时,您还需要查看已提供的 JSON。
要查看该 JSON,您可以快速将 URL 输入浏览器进行测试。您将返回一些 JSON,其中包含更好的描述您在发出请求时出错的原因。
如果您想在 PHP 中以编程方式执行此操作,则需要告诉 HTTP-wrapper used by file_get_contents
通过 $context
参数(或 setting the default context options )它不应该在 HTTP 错误(都是 400-599 代码)上返回 FALSE
,而是返回响应正文,请参阅 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 参数)
关于php - Youtube Data API v3 - Bad Request - 如何找出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854165/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!