gpt4 book ai didi

php - Youtube Data API v3 - Bad Request - 如何找出错误

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:14 24 4
gpt4 key购买 nike

我想使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com