I'm trying to pull youtube comments into a dataframe. I am able to pull details of the youtube video but a receive a 403, "The request is missing a valid API key."
error when I try to receive the commentThreads resource.
def get_service(api_name, api_version, scopes, key_file_location):
credentials = ServiceAccountCredentials.from_json_keyfile_name(
key_file_location, scopes=scopes)
# Build the service object.
service = build(api_name, api_version, credentials=credentials)
return service
# Authenticate and construct service.
service = get_service(
api_name='youtube',
api_version='v3',
scopes='https://www.googleapis.com/auth/youtube.force-ssl',
key_file_location='key_file.json')
The key_file.json
is a service account key credential.
This is there where I get the error:
response = service.commentThreads().list(
part = 'snippit',
videoId = video_id,
maxResults = 100,
order = 'relevance',
textFormat = 'plainText',
pageToken = nextPage_token).execute()
I'm not sure why I am receiving a forbidden error here as when I do the following function, it works fine:
query_results = service.search().list(part = 'snippet', q = query,
order = 'relevance',
type = 'video',
relevanceLanguage = 'en',
safeSearch = 'moderate',).execute()
question from:
https://stackoverflow.com/questions/65877328/the-request-is-missing-a-valid-api-key-with-service-account-authorization 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…