12

Is there any way that I can get the channel ID of a Youtube Channel by having its custom url (using the Youtube API) ?

Example:

A custom url is like:

https://www.youtube.com/onepiece

I want to get its channelID, so that I have the link like:

www.youtube.com/user/OnePieceUK

Nuno Pereira
  • 121
  • 1
  • 4
  • please provide full examples of the URLs – Reza Karami Sep 25 '17 at 13:56
  • 5
    See answers and comments in this possible duplicate of [**How can I get a channel ID from YouTube?**](https://stackoverflow.com/questions/14366648/how-can-i-get-a-channel-id-from-youtube) – Nope Sep 25 '17 at 13:58
  • I'm already using that request when i have the channelID or username. It's different from this case, since this is a custom URL – Nuno Pereira Sep 25 '17 at 14:17
  • I think [this answer](https://stackoverflow.com/posts/37947865/revisions) outlines the only solution I've seen. – infomofo Jan 26 '18 at 23:05

4 Answers4

3

I recently had a similar requirement where I knew the custom URL of a youtube channel and I want to get the channel id. So I went to the YouTube channel and clicked on a random video to get the Video ID from the web browser url. Once I get the Video Id, then I used the "Videos: list" API to get the video details:

https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=[VEDIO_ID]&key=[YOUR_API_KEY]

The retuned data contained all the metadata information about the video, including ChannelId :-).

It may not be the most smartest and elegant way of doing things, but it solved my purpose.

3

Elaborating on this answer of mine, I'll note here the following facts:

  1. In YouTube URLs of form https://www.youtube.com/c/NAME or https://www.youtube.com/NAME, NAME is a channel's custom URL. (See this official account from Google support.)

  2. In YouTube URLs of form https://www.youtube.com/user/NAME, NAME is a channel's user name. User names are a legacy feature of the API v3; not every channel has one attached; no channel is required to have one attached. (See this official statement from Google staff from 2013-07-11.)

The two API concepts -- custom URLs and user names -- encompass two different categories.

The public (MIT licensed) Python 3 script youtube-search.py referred by my answer quoted above is able to search the API for custom URLs and respectively query the API for user names:

$ python3 youtube-search.py --custom-url onepiece
UC6LPb3zSebrzU_0Yclpwb4Q

$ python3 youtube-search.py --user-name OnePieceUK
UC6LPb3zSebrzU_0Yclpwb4Q

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.)

stvar
  • 6,551
  • 2
  • 13
  • 28
  • your first example doesn't work: _"error: custom URL "onepiece": no associated channel found"_. But `python3 youtube-search.py --custom-url @onepiece` works. Note the `@`. I'm not sure if this is an error in the provided usage example, or an error in the python script itself, so I'm just reporting this issue to you in a comment. – naXa stands with Ukraine Jun 20 '23 at 13:39
3

try this

import requests
from bs4 import BeautifulSoup

resp = requests.get('https://www.youtube.com/onepiece')
soup = BeautifulSoup(resp.text, 'html.parser')

channel_id = soup.select_one('meta[property="og:url"]')['content'].strip('/').split('/')[-1]
Gayan Jeewantha
  • 335
  • 1
  • 3
  • 9
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '21 at 12:45
0

try my bash script:

#!/bin/bash

# Enter the YouTube channel name
echo "Enter the YouTube channel name or url:"
read channel_name

# Check if the channel name contains "youtube.com" and add it if necessary
if [[ ! "$channel_name" == *youtube.com* ]]; then
    channel_name="https://www.youtube.com/$channel_name"
fi

# Use YouTube get the channel ID
channel_id=$(curl -s -A "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0" --compressed $channel_name | grep -oP '(?<=externalId":")[^"]*(?=",)')

# Print the channel ID and original channel name
echo "The channel ID for ${channel_name##*/} is: $channel_id"
V W
  • 1
  • 1