'How to retrieve a channel id from a channel name or url
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?
Solution 1:[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()
    					Solution 2:[2]
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()
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Rafael De Alemar Vidal | 
| Solution 2 | Sandy | 
