Reputation: 995
I am trying to use the YouTube Data API to fetch channel data for particular channels. According to the documentation, I can use list (by channel ID) or list (by YouTube username)
Search by channel ID
https://youtube.googleapis.com/youtube/v3/channels?part=snippet&id={ChannelID}&key={APIKey}
// Get channel by YouTube username
https://youtube.googleapis.com/youtube/v3/channels?part=snippet&forUsername={Username}&key={APIKey}
The channel ID can be retrieved by going to YouTube in a browser and search for the channel by name and then when you select the channel; the channel ID is part of the URL like this:
https://www.youtube.com/channel/UCbTw29mcP12YlTt1EpUaVJw
.
This particular channel is for Sean Allen and his YouTube URL is:
https://www.youtube.com/SeanAllen
.
I would have thought that the YouTube username would be SeanAllen
, but it is not because, when I use that term in the API request, I get 0 results.
However, in my own case, my channel ID is UCOWdR4sFkmolWkU2fg669Gg
and the search by channel ID works to fetch my channel data. My YouTube URL is:
https://wwww.youtube.com/StewartLynch
and, if I use StewartLynch
as the username, I can use that 2nd API request to fetch the same data.
So, my question is, how does one find the correct YouTube username for a channel? Apparently it is not what is used in the channel URL.
Upvotes: 1
Views: 2700
Reputation: 6955
The issue you're raising is recurrent time and again on Stack Overflow. I myself already tackled all faces of it (just issue either of the following SO search terms: user:8327971 custom URL
or user:8327971 forUsername
).
The short story is as follows: don't rely much on YouTube user names.
User names are a legacy feature of the API v3: not every channel has one attached and no channel is required to have one attached. (See this official statement from Google staff from 2013-07-11.)
Here is what YouTube Data API returns for the two names -- SeanAllen
and StewartLynch
-- of your post:
$ python3 youtube-search.py --custom-url SeanAllen
UCbTw29mcP12YlTt1EpUaVJw
$ python3 youtube-search.py --user-name SeanAllen
UCRGhxM6u14Uv309cC0ywEqA
$ python3 youtube-search.py --custom-url StewartLynch
UCOWdR4sFkmolWkU2fg669Gg
$ python3 youtube-search.py --user-name StewartLynch
UCOWdR4sFkmolWkU2fg669Gg
As you can see, StewartLynch
is both the custom URL and the user name of the channel of which ID is UCOWdR4sFkmolWkU2fg669Gg
.
But, in case of SeanAllen
, things are not that simple since there's a channel -- UCbTw29mcP12YlTt1EpUaVJw
-- that has its custom URL set to this name; and, at the same time, there's a different channel -- UCRGhxM6u14Uv309cC0ywEqA
-- that has its attached user name set to the this very same name.
This situation is very much perennial with YouTube: many channels out there have in common this property.
The final note, which closes your question:
how does one find the correct YouTube UserName for a channel?
The Channels.list
API endpoint queried with the request parameter forUsername
does return the correct user name of a given channel (if the respective channel has attached an user name).
The ambiguity stems from the fact that URLs of form:
https://www.youtube.com/NAME
may well represent either a channel of which user name is NAME
or a channel of which custom URL is NAME
. The two cases can be set apart only by the (not-completely reliable) procedure of searching for custom URLs (which is implemented already by the script youtube-search.py
).
Above I make use of my public (MIT licensed) Python 3 script youtube-search.py
; this script is able to search the API for custom URLs and respectively query the API for user names.
Note that youtube-search.py
requires a valid API key to be passed to it as argument of the command line option --app-key
or, otherwise, passed on as the environment variable YOUTUBE_DATA_APP_KEY
. (Use the command line option --help
for brief helping info.)
Upvotes: 1