I'm building an application in which users provide a URL to a YouTube channel, and with this input I need to convert the URL to a YouTube channel ID (ex: UCUZHFZ9jIKrLroW8LcyJEQQ
). The YouTube Data API documentation appears to lack instructions on converting Custom URL's and Shortened URL's to channel ID's.
There are 4 variants of URL's for YouTube Channels:
- https://www.youtube.com/channel/UCUZHFZ9jIKrLroW8LcyJEQQ (ID-based URL)
- https://www.youtube.com/user/partnersupport (Legacy username URL)
- https://www.youtube.com/c/YouTubeCreators (Custom URL)
- https://www.youtube.com/YouTubeCreators (Shortened URL, either a Custom URL or Legacy username URL)
ID-based URL
"This is the standard URL that YouTube channels use. It uses your unique channel ID, which is the numbers and letters at the end of the URL."
Channel ID is in the URL
https://www.youtube.com/channel/<channel_id>
Legacy username URL
Usernames can be converted to channel ID's using YouTube Data API v3
https://www.youtube.com/user/<user>
Channel ID returned with API response from:
https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=<user>"&key=<API key>
Custom URL
I'm unable to find any official documentation on converting this URL variant to a channel ID. Currently, I'm using the YouTube Data API to search for the <custom URL>
value. This method is not 100% reliable.
https://www.youtube.com/c/<custom URL>
This API query performs a search, can return multiple channels, and the correct channel
may not be in the results.
https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=<custom URL>&type=channel&key=<API key>
Shortened URL
This URL variant can refer to either a Custom URL or a Legacy username URL. I currently have no method of reliably converting this variant.
https://www.youtube.com/<custom URL | User>
Questions:
- What is the most reliable method of converting a custom URL to a channel ID?
- What is the most reliable method of converting a shortened URL to a channel ID?
Note: I am NOT interested in scraping the URL to find the channel ID, as this is a violation of YouTube's terms of service. At scale, this could result in IP addresses being blacklisted from interacting with YouTube/Google services.
REF: https://support.google.com/youtube/answer/6180214?hl=en