NameError: name 'youtube' is not defined

988 views
Skip to first unread message

Mattie

unread,
Nov 15, 2016, 7:42:27 AM11/15/16
to Google APIs Explorer Users Forum

Hi!

I am trying to gather comments using the YouTube API. For this I use the example code provided on the Google developers site (I modified it a bit):

comments=[]
authors
=[]

def get_comments(youtube, video_id):
  results
= youtube.commentThreads().list(
    part
="snippet",
    videoId
=video_id,
    textFormat
="plainText"
 
).execute()

 
for item in results["items"]:
    comment
= item["snippet"]["topLevelComment"]
    author
= comment["snippet"]["authorDisplayName"]
    text
= comment["snippet"]["textDisplay"]
    comments
.append(text)
    authors
.append(author)

 
return comments
As far as I understand it, this bit of code builds a function. So now, I want to use this function to gather the comments of a video. To this end I do:

video_id='1234videoid'
get_comments
(youtube, video_id)



But this gives me an error: NameError: name 'youtube' is not defined.

Does anyone know what I should fill out instead of youtube? I should define it somehow, but I don't understand its function in this code. Any help would be greatly appreciated!

John Boswell

unread,
Nov 16, 2016, 1:20:37 PM11/16/16
to Google APIs Explorer Users Forum
Presumably that refers to a Python Google API client instance: https://google-developers.appspot.com/api-client-library/python/

sweded

unread,
Apr 17, 2017, 12:33:20 PM4/17/17
to Google APIs Explorer Users Forum
i had a similar error and corrected it by making sure i had this bit added to the code

import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()
Reply all
Reply to author
Forward
0 new messages