问题
If I make a curl
request for this URL:
https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OVP6uZZ5FSM9Ttw&key=...
I'll get the output as:
{
"kind": "youtube#channelListResponse",
"etag": "J801W-IQ15sDpy3GjDfjlUgoVxA",
"pageInfo": {
"resultsPerPage": 0
}
}
Does this mean that the YouTube channel doesn't exist? I don't get any error; how to find whether this is a valid channel or not?
Likewise, I want the list of videos of a given channel. If I make a curl
request using this URL:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCIJIhBwx4XjNUtQGZTGSVnA&maxResults=20&order=date&key=[YOUR_API_KEY]
I'll get the output as:
{
"kind": "youtube#searchListResponse",
"etag": "q5r0QewUnrg2C7BdwuxbJxb9b8c",
"regionCode": "IN",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 20
},
"items": []
}
This is a valid channel ID, but I get an empty result, not an error; how to know the search is valid or not?
回答1:
Question no. 1
For what concerns your first question:
Given a channel ID --
$CHANNEL_ID
--, test whether the respective channel exists or not.
I would recommend invoking curl
on the following URL:
https://www.googleapis.com/youtube/v3/channels?part=id&fields=items/id&id=$CHANNEL_ID&key=$APP_KEY
Note that invoking the Channels.list endpoint through the URL above -- which contains the parameters part=id
and fields=items/id
-- will return only the channel's ID.
Although not documented explicitly, tests show that you'll get back from the endpoint the ID you passed on to it, if and only if that channel does actually exist.
For example, in case of your channel ID above -- UC_x5XG1OVP6uZZ5FSM9Ttw
--, the API response is trivially empty:
{}
becase this channel does not exist (only have to click on this link to see that yourself).
On the other hand, in case of NBCNews' channel -- UCeY0bbntWzzVIaj2z3QigXg
-- the response is:
{
"items": [
{
"id": "UCeY0bbntWzzVIaj2z3QigXg"
}
]
}
showing that indeed this channel is live and kicking.
Question no. 2
For what concerns the second question of your post:
Given a channel by its ID
$CHANNEL_ID
, do list the videos of that channel.
I recommend you to consult the answer I gave recently to this very question.
In terms of curl
, you'll have to invoke the following URL:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&fields=items/contentDetails/relatedPlaylists/uploads&id=$CHANNEL_ID&key=$APP_KEY
for to obtain that channel's uploads playlist ID. For example, in the case of NBCNews' channel, the API response is:
{
"items": [
{
"contentDetails": {
"relatedPlaylists": {
"uploads": "UUeY0bbntWzzVIaj2z3QigXg"
}
}
}
]
}
Then take out that ID from the JSON response as $PLAYLIST_ID
and invoke curl
on the following URL repeatedly, implementing pagination:
https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=50&playlistId=$PLAYLIST_ID&key=$APP_KEY
.
In case of NBCNews' uploads playlist, the output of the first page would look like:
{
"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
}
}
Do notice the property nextPageToken
within the JSON response text above; the value of this property -- CAUQAA
-- would have to be passed to the second invocation of the endpoint as the parameter pageToken=CAUQAA
added to the initial URL above.
For to obtain the n
-th page, you'll extract the value of nextPageToken
from the n-1
-th page, for to pass that value to the the n
-th URL as pageToken=...
.
回答2:
There is no request in the API that will tell you 100% if a channel exits or not.
Even doing a do a video search for the channel wont help you as. If it exists then it would return the videos for that channel, if not then you get back 0 videos.
request
curl \
'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCMPpVtxOI8RtfurfvCcxgyA&order=date&key=[YOUR_API_KEY]' \
--header 'Accept: application/json' \
--compressed
response
{
"kind": "youtube#searchListResponse",
"etag": "oaf4zpPSG6Ho_cSxZedNhuVhjkw",
"nextPageToken": "CAUQAA",
"regionCode": "DK",
"pageInfo": {
"totalResults": 1721,
"resultsPerPage": 5
},
"items": [
no channel
In the event no channel exists the above code will return total results of 0
{
"kind": "youtube#searchListResponse",
"etag": "opQHhlM-mBA_i0h80B_gmHdKgCE",
"regionCode": "DK",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": []
}
Another option
Another option would be to not use the API at all. Just make a call to against YouTube itself.
If you load this page it loads the actual channel https://www.youtube.com/c/NBCNews However if you try to load https://www.youtube.com/c/lafjdajdskfadkfj it redirects you to https://www.youtube.com/error?src=404 i would think that would be something simple to detect.
来源:https://stackoverflow.com/questions/63804184/how-do-i-check-if-a-channel-exists-on-youtube