2

I'm trying to retrieve the channel id of a YouTube channel using the new v3 API. I'm using the Python client and it seems that there is no straightforward way to find the mapping of a YouTube channel URL or name to its channel Id.

Using the Python API client, it seems that I have to issue a search query of type 'channel' for the channel name, then iterate through each search result until I find a match.

I'm going to use the http://youtube.com/atgoogletalks channel as an example

search_channel_name = 'atgoogletalks'     #Parsed from http://youtube.com/atgoogletalks
search_response = youtube.search().list(
    type='channel',
    part='id,snippet',
    q=search_channel_name,
    maxResults=50
    ).execute()
for sri in search_response["items"]:
    channels_response = youtube.channels().list(
        id=sri["id"]["channelId"],
        part="id, snippet, statistics, contentDetails, topicDetails"
        ).execute()

    for cr in channels_response["items"]:
        channelname = cr["snippet"]["title"]
        if channelname.lower() == search_channel_name:
            return 'Success'

I've crawled the documentation looking for one a more straightforward way of doing this and come up short. Is there an easier way? If not, is there a plan to add this functionality to the API?

Mark Costello
  • 4,334
  • 4
  • 23
  • 26
  • Opened as a desired new enhancement with google: https://code.google.com/p/gdata-issues/issues/detail?id=3749 – Mark Costello Nov 27 '12 at 21:46
  • This is a duplicate of http://stackoverflow.com/questions/13593769/retrieve-youtube-channel-info-for-vanity-channel, and there is more discussion there. – Jeff Posnick Nov 30 '12 at 17:16

2 Answers2

1

Following YouTube Data API you could use forUsername parameter of youtube.channels.list()

Using your own example:

search_channel_name = 'atgoogletalks'
channels_response = youtube.channels().list(
        forUsername=search_channel_name,
        part="id, snippet, statistics, contentDetails, topicDetails"
).execute()
0

Sometimes the forUsername parameter doesn't return the required results. What you could do is:

from googleapiclient import build
import requests
import json

youtube = build('youtube', 'v3', developerKey=your_key_here)

channel_id = requests.get('https://www.googleapis.com/youtube/v3/search?part=id&q={search_query_here}&type=channel&key={api_key_here}').json()['items'][0]['id']['channelId']

channels_response = youtube.channels().list(id=channel_id, part='id, snippet, statistics, contentDetails, topicDetails').execute()
Sandy
  • 247
  • 3
  • 12