如果我向此URL发出curl
请求:
https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OVP6uZZ5FSM9Ttw&key=...
我将得到的输出为:
{
"kind": "youtube#channelListResponse",
"etag": "J801W-IQ15sDpy3GjDfjlUgoVxA",
"pageInfo": {
"resultsPerPage": 0
}
}
这是否意味着YouTube频道不存在? 我没有任何错误;如何找到这是否是有效频道?
同样,我想要给定频道的视频列表。如果我使用以下URL发出curl
请求:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCIJIhBwx4XjNUtQGZTGSVnA&maxResults=20&order=date&key=[YOUR_API_KEY]
我将得到的输出为:
{
"kind": "youtube#searchListResponse",
"etag": "q5r0QewUnrg2C7BdwuxbJxb9b8c",
"regionCode": "IN",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 20
},
"items": []
}
这是一个有效的频道ID,但结果为空,而不是错误;如何知道搜索是否有效?
答案 0 :(得分:0)
关于您的第一个问题:
给出一个频道ID-
$CHANNEL_ID
-,测试相应频道是否存在。
我建议在以下URL上调用curl
:
https://www.googleapis.com/youtube/v3/channels?part=id&fields=items/id&id=$CHANNEL_ID&key=$APP_KEY
请注意,通过上方包含参数part=id
和fields=items/id
的URL调用Channels.list
端点将仅返回 通道的ID。
虽然没有明确记录,但测试表明,如果且仅当该通道确实存在时,您将从端点上获取传递给它的ID。
例如,在您的频道ID高于UC_x5XG1OVP6uZZ5FSM9Ttw
的情况下,API响应几乎为空:
{}
如果此频道不存在(只需单击此link即可看到自己)。
另一方面,对于NBCNews的频道-UCeY0bbntWzzVIaj2z3QigXg
-响应为:
{
"items": [
{
"id": "UCeY0bbntWzzVIaj2z3QigXg"
}
]
}
表明该频道确实是直播和播放。
关于您的帖子的第二个问题:
通过频道ID
$CHANNEL_ID
给出一个频道,请列出该频道的视频。
我建议您咨询the answer I gave recently to this very question。
对于curl
,您必须调用以下URL:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&fields=items/contentDetails/relatedPlaylists/uploads&id=$CHANNEL_ID&key=$APP_KEY
用于获取该频道的上载播放列表ID。例如,对于NBCNews的频道,API响应为:
{
"items": [
{
"contentDetails": {
"relatedPlaylists": {
"uploads": "UUeY0bbntWzzVIaj2z3QigXg"
}
}
}
]
}
然后以$PLAYLIST_ID
的形式从JSON响应中取出该ID,并在以下URL上重复调用curl
,implementing pagination:
https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=50&playlistId=$PLAYLIST_ID&key=$APP_KEY
。
如果是NBCNews的上载播放列表,首页的输出如下:
{
"kind": "youtube#playlistItemListResponse",
"etag": "5DW9uT73DWmJtDoJ-rSw3AqHKpc",
"nextPageToken": "CAUQAA",
"items": [
{
"kind": "youtube#playlistItem",
"etag": "_X3LvLIRvEBM3RetizOGtB03ja0",
"id": "VVVlWTBiYm50V3p6VklhajJ6M1FpZ1hnLjE5NjU4N0NGQkY5M0M3MjI=",
"snippet": {
"publishedAt": "2020-09-12T06:11:59Z",
"channelId": "UCeY0bbntWzzVIaj2z3QigXg",
"title": "Watch NBC News NOW Live - September 11",
"description": "NBC News NOW is live, reporting breaking news and ...",
"thumbnails": {
...
},
"channelTitle": "NBC News",
"playlistId": "UUeY0bbntWzzVIaj2z3QigXg",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "yXO2hQXC5Dw"
}
},
"contentDetails": {
"videoId": "yXO2hQXC5Dw",
"videoPublishedAt": "2020-09-12T06:11:59Z"
},
"status": {
"privacyStatus": "public"
}
},
{
"kind": "youtube#playlistItem",
"etag": "PGyhZonOjiRzqHu7DKDPk6gcMTo",
"id": "VVVlWTBiYm50V3p6VklhajJ6M1FpZ1hnLjY2RTJFNDA4MDA0NDREQTU=",
"snippet": {
"publishedAt": "2020-09-12T02:48:48Z",
"channelId": "UCeY0bbntWzzVIaj2z3QigXg",
"title": "Gaza Sees Spike In Coronavirus Cases, Severe Shortage Of Supplies | NBC News NOW",
"description": "NBC News’ Kelly Cobiella reports on the surge in Gaza ...",
"thumbnails": {
...
},
"channelTitle": "NBC News",
"playlistId": "UUeY0bbntWzzVIaj2z3QigXg",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "I0lHV0ZVPAs"
}
},
"contentDetails": {
"videoId": "I0lHV0ZVPAs",
"videoPublishedAt": "2020-09-12T02:48:48Z"
},
"status": {
"privacyStatus": "public"
}
},
...
],
"pageInfo": {
"totalResults": 20000,
"resultsPerPage": 50
}
}
请注意上面JSON响应文本中的属性nextPageToken
;该属性的值CAUQAA
必须作为参数pageToken=CAUQAA
传递到端点的第二次调用,并添加到上述初始URL中。
要获取第n
页,请从第nextPageToken
页提取n-1
的值,以将该值传递给{{1 }}个网址为n
。
答案 1 :(得分:-1)
API中没有请求可以告诉您100%是否存在频道。
即使对频道进行视频搜索也不会帮助您。如果存在,它将返回该频道的视频,如果不存在,则返回0个视频。
curl \
'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCMPpVtxOI8RtfurfvCcxgyA&order=date&key=[YOUR_API_KEY]' \
--header 'Accept: application/json' \
--compressed
{
"kind": "youtube#searchListResponse",
"etag": "oaf4zpPSG6Ho_cSxZedNhuVhjkw",
"nextPageToken": "CAUQAA",
"regionCode": "DK",
"pageInfo": {
"totalResults": 1721,
"resultsPerPage": 5
},
"items": [
如果不存在任何通道,则上述代码将返回总结果0
{
"kind": "youtube#searchListResponse",
"etag": "opQHhlM-mBA_i0h80B_gmHdKgCE",
"regionCode": "DK",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": []
}
另一个选择是完全不使用API。只需致电YouTube本身即可。
如果您加载此页面,它将加载实际的频道https://www.youtube.com/c/NBCNews。但是,如果您尝试加载https://www.youtube.com/c/lafjdajdskfadkfj,它会将您重定向到https://www.youtube.com/error?src=404,我认为这很容易检测。 / p>