How to retrieve a channel id from a channel name o

2019-07-10 04:18发布

问题:

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?

回答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()