0

I am currently running into an issue where I am unable to retrieve a channel resource from the /v3/channels endpoint using the YouTube API (v3).

The expected behaviour is to get back a channel ID that I am then able to use with the /v3/search endpoint.

I am using Python and I am attempting to retrieve the resource by username with the forUsername query parameter.

My code looks like this:

headers = { 
    "content-type": "application/json",
}

params = { 
    "part": "snippet,contentDetails,statistics",
    "forUsername": "<USERNAME_HERE>",
    "key": api_key
}

res = requests.get(channel_endpoint, headers=headers, params=params)
if res.status_code == 200:
    data = res.json()
    if data["pageInfo"]["totalResults"] == 0:
        print("no channel found")
    print(data["items"][0]["id"])

# Response:
# {'kind': 'youtube#channelListResponse', 'etag': 'RuuXzTIr0OoDqI4S0RU6n4FqKEM', 'pageInfo': {'totalResults': 0, 'resultsPerPage': 5}}

The odd thing is that I get back a response for certain channels but not all channels.

E.g. "GoogleDevelopers" will return a coherent and meaningful response. Although, my own channel or other external channels do not return a response.

I have already checked API key permissions, restrictions and anything aligned with using the API endpoint/s or keys themselves.

I have already tried to check the username accuracy, channel visibility settings and case sensitivity.

I've gone through multiple routes already and have even considered simply scraping the channelId from the page source.

Thabang
  • 1
  • 1
  • 2
  • 2
  • Are you sure you're not confusing with [the various YouTube channel ids](https://stackoverflow.com/a/75843807)? If not, then please provide a `` value. – Benjamin Loison Jul 03 '23 at 15:54
  • Hi, I am sure I am sending the username. Here you go: @italymadeeasy (https://www.youtube.com/@italymadeeasy) – Thabang Jul 03 '23 at 16:00
  • Please read carefully the link I sent you as it solves your issue. – Benjamin Loison Jul 03 '23 at 16:02
  • @BenjaminLoison, I read carefully and have to say thank you for your work. I appreciate the help! Although, how do I distinguish between `cid`, `handle` and `username`? – Thabang Jul 03 '23 at 16:11
  • Given `a_username` you can't determine offline if it's a `cId`, `handle` (where the `@` would have been removed) or `username`, you are obliged to check what YouTube UI returns for https://www.youtube.com/@a_username, https://www.youtube.com/c/a_username and https://www.youtube.com/user/a_username to guess what identifier type it is. – Benjamin Loison Jul 03 '23 at 16:19

0 Answers0