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?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
pseudozach
  • 398
  • 6
  • 14
  • Note that, while you can't fetch a channel given a handle, the handle itself is stored inside the [channel](https://developers.google.com/youtube/v3/docs/channels/list) response (`channel -> snippet -> customUrl`). – Ben Feb 15 '23 at 22:57

2 Answers2

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"
        }
    ]
}
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • 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); – Meryan Apr 16 '23 at 15:22
  • @Benjamin Loison Your open-source api is very unstable. It throws 500 error for some handles. – JeffMinsungKim Jun 27 '23 at 08:56
  • [@JeffMinsungKim](https://stackoverflow.com/users/3252438/jeffminsungkim) Could you provide channel handles you are experiencing issues with? – Benjamin Loison Jun 27 '23 at 09:07
  • Note that if you're speaking about the unstability of the official instance itself, then consider [hosting your own instance of my API](https://github.com/Benjamin-Loison/YouTube-operational-API/blob/58ff84167b6ca4627e774ea6615db7e3692ba31d/README.md#install-your-own-instance-of-the-api). – Benjamin Loison Jun 27 '23 at 09:44
0

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

// Youtube NEW garbage handles to channel IDs
// https://stackoverflow.com/questions/75372496/how-can-i-get-youtube-channel-id-using-channel-name-url/75374713#75374713
// https://youtube.com/@AndreoBee
// https://stackoverflow.com/questions/74323173/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://stackoverflow.com/questions/28286086/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);
}
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Meryan
  • 1,285
  • 12
  • 25