3

This is a YouTube channel URL that includes Cyrillic characters in the username:
https://www.youtube.com/c/%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B/videos

I am trying to obtain the channel's id from the URL by calling the YouTube DATA API v3:

https://www.googleapis.com/youtube/v3/channels?key=[YouTubeAPIkey]&forUsername=%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B&part=id

But the call returns no data.

For reference, "https://www.youtube.com/c/besogontv/videos" returns a valid result:

https://www.googleapis.com/youtube/v3/channels?key=[YouTubeAPIkey]&forUsername=besogontv

Just to see if it may work, I tried decoding the URL encoding and then re-encoding to UTF8, but it didn't make a difference.

Is there some character encoding issue I'm missing?

bLight
  • 803
  • 7
  • 23

1 Answers1

3

If you'll issue the following command (at any GNU/Linux bash prompt):

$ wget \
--quiet \
--output-document=- \
--content-on-error \
"https://www.googleapis.com/youtube/v3/channels?key=$APP_KEY&id=UCk8LWzqGcHz21FWysiXuCHw&part=brandingSettings,contentDetails,id,snippet,statistics,status,topicDetails&maxResults=1"

you'll see that лучшиедокументальныефильмы is not the channel's user name, but its customUrl!

The forUsername property does not function for a given channel's custom URL since these URLs are not guaranteed to uniquely represent any given channel.

Do convince yourself by querying on Google's issue tracker for either of these two phrases channels forusername or vanity URL to see the terse/raw official responses users got from Google's staff.

Indeed, at times, the official docs and staff responses do lack useful/meaningful clear-cut specifications and/or formulations. (I already experienced all these myself too!)

As a final note, you may scrape out of the HTML page obtained from https://www.youtube.com/c/лучшиедокументальныефильмы the channel ID of your interest, but please bear in mind that this activity is forbidden by Google, as per its DTOS docs:

Scraping

You and your API Clients must not, and must not encourage, enable, or require others to, directly or indirectly, scrape YouTube Applications or Google Applications, or obtain scraped YouTube data or content. Public search engines may scrape data only in accordance with YouTube's robots.txt file or with YouTube's prior written permission.

Instead of scraping, I'd recommend using the Search.list API endpoint, invoked with the q parameter being лучшиедокументальныефильмы and the type parameter being channel (if you're able to cope with the fuzziness implied).


Update upon answering to a related SO question

Here is a simple Python3 script implementing the functionality that you're looking for. Applying your custom URL to this script produces the expected result:

$ python3 youtube-search.py \
--custom-url Лучшиедокументальныефильмы \
--app-key ...
UCk8LWzqGcHz21FWysiXuCHw

$ python3 youtube-search.py \
--user-name Лучшиедокументальныефильмы \
--app-key ...
youtube-search.py: error: user name "Лучшиедокументальныефильмы": no associated channel found

Note that you have to pass to this script your application key as argument to the command line option --app-key (use --help for brief help info).

stvar
  • 6,551
  • 2
  • 13
  • 28
  • 1
    Then is it possible to determine the channel id from the vanity channel name without scraping the page, just based on the vanity URL? – bLight Jun 25 '20 at 16:20
  • 1
    Without scraping (thus in compliance with DTOS) only using `Search.list` endpoint when doing it programatically -- if your algorithms can cope with the fuzziness of the result set obtained. – stvar Jun 25 '20 at 16:48
  • 1
    If doing it manually, you may use the search function on Youtube's Web UI itself. In this case, enter via copy-paste the search term `лучшиедокументальныефильмы`, then apply (again manually) the filter for type being `channel`. The search result obtained in the browser lists the normalized channel URL at the top position: `https://www.youtube.com/channel/UCk8LWzqGcHz21FWysiXuCHw`. The channel ID is simply the entire string at the end of the base URL `https://www.youtube.com/channel/`. – stvar Jun 25 '20 at 16:49
  • This *manual activity* is perfectly legal w.r.t. DTOS for one to obtain a channel's ID -- to be used further invoking API endpoints programmatically. – stvar Jun 25 '20 at 16:56
  • I was hoping there was an API to convert between the two, using the search API means that there may be false positives (for example a popular name). – bLight Jun 25 '20 at 20:04
  • In any case, if designing an app that indeed needs to *compute* an channel ID from a non-unique name like the one mentioned above, there is still the possibility for something like the following: invoke `Search.list`, and provide to the human that has to *decide* if the respective name do associate with the channel he/she is looking up for sufficient information (derived from the info obtained from `Search.list`) such that his/her decision to be made as easily as possible. – stvar Jun 25 '20 at 21:03
  • This can be thought as a specific enhancement (niche specialization) of the search function under the Youtube's Web UI itself. I think this is legal too (nota bene: am not a lawyer!) -- but one has to scrutinize carefully the DTOS docs before publishing such an app. – stvar Jun 25 '20 at 21:09
  • @stvar thanks for clarifying the legal aspects of a manual fix. I just had one channel ID that "played up" with ```channels().list()``` and ```forUsername```. So looking for it manually in the meta data in a browser is a real quick solution. – Simone May 26 '22 at 14:00