1

I'm new to working with the YouTube API v3, and StacOverflow in general, so sorry for any mistakes.

Right now I am having trouble understanding the forUsername parameter. For some channels, such as YouTube or H2ODelerious, I am able to find the correct channel. When it comes to using channels such as leagueoflegends or playoverwatch (both of these being the names of the channels when it comes to composing a URL of form https://www.youtube.com/c/...) I do not find the correct channel and neither find no channel at all, nor one with no views or subscribers.

Here is my code:

channelStats = testConnection.channels().list(
    part ='statistics',
    forUsername ='Youtube'
)
response = channelStats.execute()

If anyone knows whether there is another way to properly identify the correct channel while using the Channels.list method, please let me know. (Am using YouTube Data API v3 through Google's Client Library for Python and Python 3.8.)

stvar
  • 6,551
  • 2
  • 13
  • 28
Sean
  • 11
  • 2

1 Answers1

5

I did answered thoroughly to precisely your kind of inquiry quite recently. I'd warmly recommend you to absorb these two posts:

  1. How to get a YouTube channel ID from the channel's username which includes Cyrillic characters and

  2. Obtaining a channel id from a youtube.com/c/xxxx link?

Here, I'll only emphasize that the forUsername request parameter has the following official specification:

forUsername (string)

The forUsername parameter specifies a YouTube username, thereby requesting the channel associated with that username.

But, the name NAME under an URL of form:

https://www.youtube.com/c/NAME

is not necessarily the YouTube username of the respective YouTube channel. NAME is that channel's custom URL, a different category.

According to an official Google staff post from 2013-07-11, it's not a requisite that every channel have attached an username. Google's issue tracker records even a forCustomUrl feature request that has been rejected for the time being (at least for the API's road map of 2020).

The second post I quoted above provides an algorithm that'll produce the channel ID associated to a given custom URL. Moreover, that post also provides a Python function that implements the algorithm.

A public (MIT licensed) Python 3 script -- youtube-search.py -- is linked to that post too; the script implements both the custom URL algorithm and the forUsername API querying.

This script produces the following outputs when invoked with each of the searched names your post above contains:

$ python3 youtube-search.py --user-name YouTube
UCBR8-60-B28hp2BmDPdntcQ

$ python3 youtube-search.py --custom-url YouTube
youtube-search.py: error: custom URL "YouTube": no associated channel found

$ python3 youtube-search.py --user-name H2ODelerious
youtube-search.py: error: user name "H2ODelerious": no associated channel found

$ python3 youtube-search.py --custom-url H2ODelerious
youtube-search.py: error: custom URL "H2ODelerious": no associated channel found

$ python3 youtube-search.py --search-term H2ODelerious --type=channel
UCClNRixXlagwAd--5MwJKCw: H2ODelirious
UCagP0Z1PVAAOpYV4HKy6w9w: H2o Delerious
UC6x4XQ6cqBzxGlSlCwcyUBw: H2O Delerious
UCzOeqwelmnbBDf3bIHde0EQ: H2O Delerious
UCLmpVRE4XIEem_1EwHxqtXQ: H2O Delerious plays
UCxxWGzT90DG1gk2pnvcRt2w: h2o delerious fan artist
UCYH7TzSISrC-2Mfy3TyjUDQ: H2O DELERIOUS Flores
UCfRjtX1qzFbCptULo529HsA: Jbz2swag
UCYd0SDTRQ54K1UQ-OkQd_7w: Jehowen
UCjozfwXXP7bEGPDFpAtgsYQ: LittleCool Me

$ python3 youtube-search.py --user-name leagueoflegends
UCvewcgPDiYrVNiyh8kpOnFQ

$ python3 youtube-search.py --custom-url leagueoflegends
UC2t5bjwHdUX4vM2g8TRDq5g

$ python3 youtube-search.py --user-name playoverwatch
UCM9KGdihR14QHFeyOsA-kgA

$ python3 youtube-search.py --custom-url playoverwatch
UClOf1XXinvZsy4wKPAkro2A

Note that youtube-search.py requires a valid API key to be passed to it as argument of the command line option --app-key or, otherwise, passed on as the environment variable YOUTUBE_DATA_APP_KEY. (Use the command line option --help for brief helping info.)

Also note that among the channels resulted from invoking the search script through:

$ python3 youtube-search.py --search-term H2ODelerious --type channel,

only two have the customUrl property set, as follows:

$ python3 youtube-search.py --channel-url UCClNRixXlagwAd--5MwJKCw
h2odelirious

$ python3 youtube-search.py --channel-url UCYd0SDTRQ54K1UQ-OkQd_7w
jehowen
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
stvar
  • 6,551
  • 2
  • 13
  • 28
  • 1
    for others who read this answer: you have to install google api lib for python: `pip install --upgrade google-api-python-client` – Homayoon Ahmadi Jan 17 '22 at 15:25