How can I get all videos ID's from a Youtube channel

Viewed 23118
8 Answers

My solution uses Python, Google API, and paginates to reach all the video Ids.

  • Create a .env file to store your Developer Key and put it in the .gitignore file
  • The parameter "forUserName" should be set with the name of the Youtube Channel (username). Alternatively, you can use the channel id, setting the parameter "id", instead of "forUserName".
import os, sys
import googleapiclient.discovery
from decouple import config
def main():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    DEVELOPER_KEY = config('API_KEY')

    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, developerKey = DEVELOPER_KEY)

    request = youtube.channels().list(
        part="contentDetails",
        forUsername="username_here",
        # id="uiyiouyiouy",
    )

    response = request.execute()
    for item in response['items']:
        playlistId = item['contentDetails']['relatedPlaylists']['uploads']
        nextPageToken = ''
        while (nextPageToken != None):
            playlistResponse = youtube.playlistItems().list(
                part='snippet',
                playlistId=playlistId,
                maxResults=25,
                pageToken=nextPageToken
            )
            playlistResponse = playlistResponse.execute()
            for idx, playlistItem in enumerate(playlistResponse['items']):
                print(idx, playlistItem['snippet']['resourceId']['videoId'])
            if 'nextPageToken' in playlistResponse.keys():
                nextPageToken = playlistResponse['nextPageToken']
            else:
                nextPageToken = None

if __name__ == "__main__":
    main()

Example for the .env file

API_KEY=<Key_Here>
Related