0

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:

  1. https://www.youtube.com/channel/UCUZHFZ9jIKrLroW8LcyJEQQ (ID-based URL)
  2. https://www.youtube.com/user/partnersupport (Legacy username URL)
  3. https://www.youtube.com/c/YouTubeCreators (Custom URL)
  4. 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:

  1. What is the most reliable method of converting a custom URL to a channel ID?
  2. 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

stvar
  • 6,551
  • 2
  • 13
  • 28
  • 1
    Please follow the [series of answers](https://stackoverflow.com/search?q=user%3A8327971+custom+url) I gave recently to this very issue. (I cannot repeat those answers here -- that would be against SO guidelines.) The most (almost 100%) reliable way to obtain channel IDs from custom URLs is described by [Obtaining a channel id from a youtube.com/c/xxxx link](https://stackoverflow.com/a/63081162/8327971). Shortened URLs are [custom URLs](https://support.google.com/youtube/answer/2657968). – stvar Mar 18 '21 at 16:10
  • Thank you for your response, I had not thought about using the `channels.list` API endpoint to compare the `customUrl` value. In contrast to what you mentioned, according to https://support.google.com/youtube/answer/6180214?hl=en shortened URL's can refer to either a custom URL or a Legacy username URL. – Michael Hill Mar 18 '21 at 16:29
  • Indeed you're right: in case of an URL of form `https://youtube.com/NAME`, `NAME` could be an *user name*, if it's not a *custom URL*. That is that, one has to consider `NAME` as an custom URL by look it up, using the *almost reliable* algorithm I quoted above; in case (1) it's a custom URL then OK; otherwise, (2) in case the algorithm fails, then `NAME` could still be a custom URL or an user name. – stvar Mar 18 '21 at 18:55
  • The alternative (2) continues by checking whether `NAME` is a user name using the 100% reliable `Channels.list` query for `forUsername`. In the case the endpoint indicates that `NAME` is an user name then OK; else `NAME` is not an user name, but could still be a valid custom URL that the algorithm at (1) was not able to identify. – stvar Mar 18 '21 at 18:57
  • I have to correct my latest comment above (*The alternative (2) continues by...*) as follows: In the case `Channels.list` indicates `NAME` not to be an user name, then FAIL; otherwise, check whether the [`customUrl`](https://developers.google.com/youtube/v3/docs/channels#snippet.customUrl) property of the channel returned by `Channels.list` is equal to `NAME` or not. In case of equality, then OK; otherwise FAIL: `NAME` could still be a custom URL of a channel that we cannot know by current API means (but which is known to the YouTube's back-end). – stvar Mar 18 '21 at 20:18

0 Answers0