How to map youtube handles to channel IDs
Asked Answered
N

4

4

Youtube recently rolled out handles feature where they gave users youtube.com/@xxx type usernames, when visited these URLs show user's channel but I can't find any documentation or reference in API repositories.

How to extract youtube user channel ID from their handle?

Normi answered 4/11, 2022 at 21:31 Comment(1)
Note that, while you can't fetch a channel given a handle, the handle itself is stored inside the channel response (channel -> snippet -> customUrl).Boast
P
5

One more time YouTube Data API v3 doesn't provide a basic feature.

I recommend you to try out my open-source YouTube operational API. Indeed by fetching https://yt.lemnoslife.com/channels?handle=@HANDLE you will get the YouTube channel id you are looking for in item["id"].

For instance with the YouTube channel handle @WHO, you would get:

{
    "kind": "youtube#channelListResponse",
    "etag": "NotImplemented",
    "items": [
        {
            "kind": "youtube#channel",
            "etag": "NotImplemented",
            "id": "UC07-dOwgza1IguKA86jqxNA"
        }
    ]
}
Poirier answered 5/11, 2022 at 2:45 Comment(4)
Thank you I used the following to parse the JSON string var JSON_Object = JObject.Parse(JSON_String); // var ChannelId = JSON_Object["items"][0]["id"].ToString(); return await ChannelVideosInfo(ChannelId);Falconry
@Benjamin Loison Your open-source api is very unstable. It throws 500 error for some handles.Fundus
@JeffMinsungKim Could you provide channel handles you are experiencing issues with?Poirier
Note that if you're speaking about the unstability of the official instance itself, then consider hosting your own instance of my API.Poirier
F
0

Wasted about 2 hours to derive the following working code/fix...

// Youtube NEW garbage handles to channel IDs
// https://mcmap.net/q/102624/-how-can-i-get-youtube-channel-id-using-channel-name-url/75374713#75374713
// https://youtube.com/@AndreoBee
// https://mcmap.net/q/103330/-how-to-map-youtube-handles-to-channel-ids
// One more time YouTube Data API v3 doesn't provide a basic feature.
// https://yt.lemnoslife.com/  Open Source YouTube API
/* sample JSON returned
        {
            "kind": "youtube#channelListResponse",
            "etag": "NotImplemented",
            "items": [
                {
                    "kind": "youtube#channel",
                    "etag": "NotImplemented",
                    "id": "UC9rnrMdYzfqdjiuNXV7q8oQ"
                }
            ]
        }
 */
/// <summary>
/// Return Videos from ChannelId (derived from new Google garbage handle to channel)
/// 20230415
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public async Task<List<YouTubeInfo>> UserVideosViaNewHandleToChannelId(String UserName)
{
        string URI = "https://yt.lemnoslife.com/channels?handle=" + UserName;
        //
        // https://swimburger.net/blog/dotnet/configure-servicepointmanager-securityprotocol-through-appsettings
        // https://mcmap.net/q/87700/-default-securityprotocol-in-net-4-5
        // without the following Microsoft garbage now (httpClient.GetStringAsync(URI);)   will crash.
        System.Net.ServicePointManager.SecurityProtocol |=
                                SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        //
        // https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/console-webapiclient
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
        httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

        //
        // "https://api.github.com/orgs/dotnet/repos"
        var JSON_String = await httpClient.GetStringAsync(URI);
        Console.Write(JSON_String);
        // https://peterdaugaardrasmussen.com/2022/01/18/how-to-get-a-property-from-json-using-selecttoken/
        var JSON_Object = JObject.Parse(JSON_String);
        //
        var ChannelId = JSON_Object["items"][0]["id"].ToString();
        return await ChannelVideosInfo(ChannelId);
}
Falconry answered 16/4, 2023 at 15:26 Comment(0)
Q
0

As of December 2023, the YouTube API still doesn't have an endpoint that supports pulling channel data by handle.

Using PHP, I did find a bit of a workaround.

use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\HttpClient\HttpClient;

public function getChannelIDFromUrl($url) {
 $httpClient = new HttpBrowser(HttpClient::create());
            $crawler = $httpClient->request('GET', $url);
            $channelUrl = $crawler->filterXPath('//meta[@property=\'og:url\']')
                ->attr('content');
            return explode('/', $channelUrl)[4];
}

Note: $url returns a string with the @ handle in it.

Quartering answered 1/12, 2023 at 10:3 Comment(0)
D
0

now you can use the new parameter forHandle (docs)

The forHandle parameter specifies a YouTube handle, thereby requesting the channel associated with that handle. The parameter value can be prepended with an @ symbol. For example, to retrieve the resource for the "Google for Developers" channel, set the forHandle parameter value to either GoogleDevelopers or @GoogleDevelopers.

Dodder answered 10/2 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.