Define 'api' traceback errors in Python

Viewed 61

I am trying to search YouTube for videos using YouTube v3 API in Python with the requirements in the code, but I'm getting this error. enter image description here

How can I prevent this from happening? This code is from Google too. You can edit the code on the Google website here. I'm a total noob at this so if you have any information I must know for the future, please do say so. Thanks

# -*- coding: utf-8 -*-

# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

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

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

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.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 = api.youtube.search().list(
        part="snippet",
        location="United States",
        maxResults=5,
        order="date",
        q="programming",
        regionCode="US",
        relevanceLanguage="en",
        safeSearch="none",
        videoDimension="any",
        videoDuration="any"


        
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

error number two

enter image description here

1 Answers

Yeah your code may be from google but you have renamed something.

All your calls need to go though the YouTube serivce object

 youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

Yet you are calling it using api

request = api.youtube.search().list(

it should be

request = youtube.search().list(

Check the code from google again and becameful when you rename things if you don't understand what there used for.

# -*- coding: utf-8 -*-

# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

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

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

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 = "YOUR_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.search().list(
        part="snippet",
        location="United States",
        maxResults=5,
        order="date",
        q="Programming",
        regionCode="US",
        relevanceLanguage="en",
        safeSearch="none",
        videoDimension="any",
        videoDuration="any"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

Error number 2

You need to check the parameters you are sending remove location thats not a valid location

enter image description here

Related