Home > Blockchain >  Using Youtube API to return parts of parts [Python]?
Using Youtube API to return parts of parts [Python]?

Time:08-28

Working with the Youtube API v3 to try and pull data from a channel's playlists. I can see their playlists (or at least the default favorites, likes, and uploads) and want to pull more specific content from them. However, when I try to call Youtube.channels().list and then specify part as contentDetails.relatedPlaylists.uploads, I get an unknown part error, which is confusing because Youtube API says it should work and return a string, which shouldn't have an error printing. What am I doing wrong here? Will include code below.

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "./CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.channels().list(
        part="contentDetails.relatedPlaylists.uploads",
        id="UCtcUKdcAXfVCo5YlgBMkfjA"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

CodePudding user response:

Just use part=contentDetails to comply with Channels: list YouTube Data API v3 documentation and print(response["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"]) as you would notice that response is just a Python dictionnary and by priting response or looking at the documentation you can guess the path of keys to access the value you are looking for, that is items/0/contentDetails/relatedPlaylists/uploads.

  • Related