Reputation: 141
I am currently using the YouTube Data Channel API, which allows me to retrieve the Channel ID of any given username.
https://www.googleapis.com/youtube/v3/channels?key={KEY HERE}&forUsername={USERNAME}&part=id,snippet&order=date
It works for the majority of usernames, but for some reason it chooses not to work for others.
For example, if you visit the URL: https://youtube.com/impaulsive
, we can see that the username must be impaulsive
.
However, if you search that through the Channel API via the forUsername
attribute, it returns no results - even though it is their actual username and directs you to their official channel.
Is there a way to retrieve the Channel ID from a username?
Upvotes: 3
Views: 4935
Reputation: 28
No need to use any type of api, you can directly get the channel id using axios.
I have found this way from doing inspect of a youtube channel.
here is the code to get channel id using channel username:
to install axios on node.js
npm install axios
const axios = require('axios');
let Url = "https://www.youtube.com/@triggeredinsaan"
let channelUrl = Url.replace("/user", ""); //this will replace /user to the supported format
//supported format: https://www.youtube.com/@username
var url = new URL(channelUrl)
const { data } = await axios.get(url.href)
let Id = data.split(`<link rel="canonical" href="`)[1];
let Result = Id.split(`">`)[0];
return console.log(ResultId); //Your channel id returns in provided format: https://www.youtube.com/channel/Id
Upvotes: 0
Reputation: 6965
Your issue is very much recurrent with YouTube Data API (here, on SO was recently tackled several times: just issue the following SO search query: [youtube-data-api] forUsername
).
Basically, you have to acknowledge two things:
User names are a legacy feature of the API v3; not every channel has one attached; no channel is required to have one attached. (See this official statement from Google staff from 2013-07-11.)
If you come across URLs of form https://www.youtube.com/c/NAME
(or even https://www.youtube.com/NAME
), then NAME
is not necessarily (though it could be) the YouTube user name of an YouTube channel. NAME
is a channel's custom URL
, a different category. (See this official account from Google support.)
Your example -- impaulsive
-- fits well with point 2 above, because it is the custom URL of the channel of which ID is UCGeBogGDZ9W3dsGx-mWQGJA
, yet is not the user name of any channel:
$ python3 youtube-search.py --custom-url impaulsive
UCGeBogGDZ9W3dsGx-mWQGJA
$ python3 youtube-search.py --user-name impaulsive
youtube-search.py: error: user name "impaulsive": no associated channel found
The script youtube-search.py
used above is a public (MIT licensed) Python 3 program (that I developed) implementing an algorithm that searches for custom URLs and also is querying 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: 3