0

A bunch of SO answers shows that the YouTube API v3 doesn't yet provide a trivial way to get a YouTube channel by its handle. A workaround is to perform a search with the handle as a search term :

  • Answer of How to get Youtube channel details using Youtube data API if channel has custom url
  • Answer of YouTube Data API Get Channel by Handle
  • Answer of How to map youtube handles to channel IDs
  • Answer of How can I get YouTube channel ID using channel name URL?

Then, to retrieve only the first result being a channel, this piece of code can be used:

SearchResource.ListRequest SRLR = this.YouTubeService.Search.List("snippet");
SRLR.Q = Handle;
// SRLR.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.None;
SRLR.MaxResults = 1;
// SR contains the first result that *should* be the searched channel
// otherwise an exception is thrown 
SearchResult SR =
    SRLR.Execute().Items.Where(x => x.Id.Kind == "youtube#channel").Single();

Then, this result can be checked by retrieving the channel by its Id and comparing the search term with the actual channel handle.

ChannelsResource.ListRequest CRLR = this.YouTubeService.Channels.List(part);
CRLR.Id = SR.Snippet.ChannelId;
Channel Channel = CRLR.Execute().Items.Single();

if (Channel.Snippet.CustomUrl != Handle)
    throw new System.Exception($"No channel with handle \"{Handle}\"");

Finally, it is possible to process the channel itself.

This works very well with some handles like @aha or @Queen.

But the search doesn't return a channel for the handle @lachaiseproductions1654. Instead, a list of 5 unrelated videos is returned.

However, The channel appears in a YouTube search.

What's more, a test suggested by Benjamin Loison in the comments shows that the HTTP call itself works, both from a web browser and with the cURL utility.

$ curl 'https://www.googleapis.com/youtube/v3/search?q=@lachaiseproductions1654&type=channel&key=API_KEY'
{
  "kind": "youtube#searchListResponse",
  "etag": "e7MNUTzPTPLLstR3AudmySikeJI",
  "regionCode": "FR",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "ELI76vrmCruoLt7k4Bb3ffs4V8s",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCDSqPuB6YKO4E5FjKP1R23Q"
      }
    }
  ]
}

In the code, using YouTube Api, I tried to disable SafeSearch (commented out above) just in case although there should be no reason, but it had no effect.

Yet this channel can be reached by its id (UCDSqPuB6YKO4E5FjKP1R23Q). Why did I miss?

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • 1
    https://www.googleapis.com/youtube/v3/search?q=@lachaiseproductions1654&type=channel&key=API_KEY returns correctly as the first result the channel you are looking for, right? – Benjamin Loison Jul 27 '23 at 18:45
  • @BenjaminLoison, I should have started with that. I'm using right now another device, so I can't confirm yet it will work in the dev environment. I'll give an update asap. Thanks. – Amessihel Jul 27 '23 at 19:13
  • @BenjaminLoison, I guess I shound use a sniffer like tcpdump or Wireshark to understand what's going on using the .NET YouTube Api. – Amessihel Aug 03 '23 at 15:58
  • I'm not keen on .NET YouTube API library, maybe just working with its source code if you have access to it would be simpler. – Benjamin Loison Aug 03 '23 at 16:02

0 Answers0