19
votes

I would like to fetch details of a YouTube channel which has a custom URL, like https://www.youtube.com/c/pratiksinhchudasamaisawesome.

Custom channel URLs follow this format: https://www.youtube.com/c/{custom_channel_name}.

I can fetch the details of YouTube channels by Channel ID and username without any issues. Unfortunately, I need to use the custom channel URL which is the only time I encounter this issue.

I developed my app few months ago, and the custom channel URL was working up until a few days ago. Now, the YouTube data API does not return anything for the YouTube custom channel URL if I try get details using their custom name.

To get the details of this channel: https://www.youtube.com/user/thenewboston, for example, the request would be:

GET https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=thenewboston&key={YOUR_API_KEY}

Response

200
- SHOW HEADERS -
{
 "kind": "youtube#channelListResponse",
 "etag": "\"zekp1FB4kTkkM-rWc1qIAAt-BWc/8Dz6-vPu69KX3yZxVCT3-M9YWQA\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 5
 },
 "items": [
  {

   "kind": "youtube#channel",
   "etag": "\"zekp1FB4kTkkM-rWc1qIAAt-BWc/KlQLDlUPRAmACwKt9V8V2yrOfEg\"",
   "id": "UCJbPGzawDH1njbqV-D5HqKw",
   "snippet": {
    "title": "thenewboston",
    "description": "Tons of sweet computer related tutorials and some other awesome videos too!",
    "publishedAt": "2008-02-04T16:09:31.000Z",
    "thumbnails": {
     "default": {
      "url": "https://yt3.ggpht.com/--n5ELY2uT-U/AAAAAAAAAAI/AAAAAAAAAAA/d9JvaIEpstw/s88-c-k-no-rj-c0xffffff/photo.jpg"
     },
     "medium": {
      "url": "https://yt3.ggpht.com/--n5ELY2uT-U/AAAAAAAAAAI/AAAAAAAAAAA/d9JvaIEpstw/s240-c-k-no-rj-c0xffffff/photo.jpg"
     },
     "high": {
      "url": "https://yt3.ggpht.com/--n5ELY2uT-U/AAAAAAAAAAI/AAAAAAAAAAA/d9JvaIEpstw/s240-c-k-no-rj-c0xffffff/photo.jpg"
     }
    },
    "localized": {
     "title": "thenewboston",
     "description": "Tons of sweet computer related tutorials and some other awesome videos too!"
    }
   }
  }
 ]
}

It works perfectly.

Now we have to get details of these channels:

Then we get:

GET https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=annacavalli&key={YOUR_API_KEY}

Response

200
- SHOW HEADERS -
{
 "kind": "youtube#channelListResponse",
 "etag": "\"zekp1FB4kTkkM-rWc1qIAAt-BWc/TAiG4jjJ-NTZu7gPKn7WGmuaZb8\"",
 "pageInfo": {
  "totalResults": 0,
  "resultsPerPage": 5
 },
 "items": [
 ]
}

This can be easily reproduced using the API explorer.

4
This looks like it could be a bug with their API. Why not report it and see if Google will fix it for you?Laurel
It's not a bug, but rather a missing feature. Usernames (which come from old times, before YouTube and Google Plus integration) are not the same as custom channel identifiers, so it is expected that forUsername filter does not work that way.jkondratowicz
Does the Youtube API still not support this in 2018?William Bing Hua
It is 2021 and this feature is still not supported (or at least not working for me when using a forUserName or id attribute as a lookup)Learner

4 Answers

20
votes

Simplest solution, using API only, is to just use Search:list method of YouTube Data API. From what I can tell (mind you, this is from my own research, official docs say nothing on this subject!), if you search using the custom URL component, with "channel" result type filter and "relevance" (default) sorting, first result should be what you're looking for.

So the following query gets 16 results, with the first one being the one you're looking for. Same goes for all other custom channel URLs I tested, so I think this is the most reliable way of doing this.

GET https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=annacavalli&type=channel&key={YOUR_API_KEY}

The other idea is just scraping YouTube page at the custom URL, where you can find ChannelID in one of the meta tags in HTML code. But that's ineffective, unreliable and AFAIK in violation of YouTube terms of use.

Edit: Well, it returns no results for smaller channels, so it's not reliable at all.

1
votes

Workaround

Expanding off of @jkondratowicz answer, using the search.list in combination with channels.list you can most of the time resolve the channel from the custom url value.

The channel resource has a property customUrl so if we take the channels from the search.list results and get that extra detail about them from the channels.list it is possible to try and match up the custom url value with the customUrl property.

A working JavaScript method here, just replace the api key with your own. Though it is still not perfect, this tries the first 50 channels returned. More could be done with paging and pageTokens.

function getChannel(customValue, callback) {
    const API_KEY = "your_api_key"
    $.ajax({
        dataType: "json",
        type: "GET",
        url: "https://www.googleapis.com/youtube/v3/search",
        data: {
            key: API_KEY,
            part: "snippet",
            q: customValue,
            maxResults: 50,
            order: 'relevance',
            type: 'channel'
        }
    }).done(function (res) {
        const channelIds = [];
        for (let i=0; i<res.items.length; i++) {
            channelIds.push(res.items[i].id.channelId);
        }

        $.ajax({
            dataType: "json",
            type: "GET",
            url: "https://www.googleapis.com/youtube/v3/channels",
            data: {
                key: API_KEY,
                part: "snippet",
                id: channelIds.join(","),
                maxResults: 50
            }
        }).done(function (res) {
            if (res.items) {
                for (let i=0; i<res.items.length; i++) {
                    const item = res.items[i];
                    if (item.snippet.hasOwnProperty("customUrl") && customValue.toLowerCase() === item.snippet.customUrl.toLowerCase()) {
                        callback(item);
                    }
                }
            }
        }).fail(function (err) {
            logger.err(err);
        });
    }).fail(function (err) {
        logger.err(err);
    });
}

A good example using it with https://www.youtube.com/c/creatoracademy.

getChannel('creatoracademy', function (channel) {
    console.log(channel);
});

However, it is still unreliable as it depends on if the channel comes back in the original search.list query. It seems possible that if the custom channel url is too generic that the actual channel may not come back in the search.list results. Though this method is much more reliable then depending on the first entry of search.list to be the right one as search results don't always come back in the same order.

Issue

There has been at least three feature requests to Google in the past year requesting for an additional parameter for this custom url value but they were all denied as being infeasible. Apparently it is too difficult to implement. It was also mentioned as not being on their roadmap.

Resources

0
votes

An alternative way is to use a parser (PHP Simple HTLM DOM parser, for the example below : PHP Simple HTML DOM Parser) :

<?php
$media_url = 'https://www.youtube.com/c/[Channel name]';

$dom = new simple_html_dom();
$html = $dom->load(curl_get($media_url));
if (null !== ($html->find('meta[itemprop=channelId]',0))) {
    $channelId = $html->find('meta[itemprop=channelId]',0)->content;
}
?>

(Using Youtube api's "search" method has a quota cost of 100)

0
votes

I'm assuming that only channels with videos uploaded by the channel owner will be of interest. This is accidentally convenient since my method doesn't work with 0 video channels anyway.

Given a channel's url, my method will get the beautifulsoup HTML object of that channel's videos tab, and scrape the HTML to find the unique channel id. It'll then reconstruct everything and give back the channel url with the unique channel id.

your_channel_url = 'Enter your channel url here'
channel_url = your_channel_url.strip("https://").strip("featured")
https = "https://"
channel_vids_tab = https + channel_url + '/videos'

import requests
from bs4 import BeautifulSoup

source = requests.get(channel_vids_tab).text
soup = BeautifulSoup(source, "html.parser")

a = soup.find('body').find('link')['href']
channel_id = a.split('/')[-1]

print(a)
print(channel_id)

This method bypass the headache of one channel having different /user and /c url (for example /user/vechz and /c/vechz vs /c/coreyms and /user/schafer5 leading to the same page). Though you need to manually enter the url at first, it can be easily automated.

I'm also fairly confident that if a channel has 0 videos, this line of thinking can also apply for the playlist created BY the channel owner, and only needs a little tweaking. But if there's 0 videos or playlist created by the channel ... who knows