Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get youtube username from channel id

How to get the username of the channel from using the youtube channel id ?

E.g. UCnpaBg-u_kHwzuPyaMcyJ0w is the Channel ID for Sony Max

now how to get username "maxindia" from Youtube v3 Api

like image 914
Vijay C Avatar asked Oct 01 '22 01:10

Vijay C


2 Answers

Here is a Python solution. It tries to find out the channel info from the video ID. Some newer channels only have a channel ID and no username.

import requests

API_KEY = "..."

def get_channel_id(vid):
    url = "https://www.googleapis.com/youtube/v3/videos?id={vid}&key={api_key}&part=snippet".format(
        vid=vid, api_key=API_KEY
    )
    d = requests.get(url).json()
    return d['items'][0]['snippet']['channelId']

def get_channel_title(cid):
    url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={cid}&key={api_key}".format(
        cid=cid, api_key=API_KEY
    )
    d = requests.get(url).json()
    return d['items'][0]['snippet']['channelTitle']

def main():
    video_id = "..."
    channel_id = get_channel_id(video_id)
    channel_title = get_channel_title(channel_id)
    print(channel_id)
    print(channel_title)
like image 96
Jabba Avatar answered Oct 14 '22 03:10

Jabba


When using the API (for example on developers.google.com), use part=snippet and the channelId you provided, then the channelTitle can be found in one of the items/snippet objects.

Here is the truncated response:

{
  ...
  "items": [
    {
      ...
      "snippet": {
        ...
        "channelTitle": "maxindia",
        ...
      }
    }
}
like image 23
Thurion Avatar answered Oct 14 '22 02:10

Thurion