YouTube analytics API rows empty

家住魔仙堡 提交于 2019-12-23 19:47:10

问题


I know this question has been answered before, but I seem to have a different problem. Up until a few days ago, my querying of YouTube never had a problem. Now, however, every time I query data on any video the rows of actual video data come back as a single empty array.

Here is my code in full:

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

import os
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
import pandas as pd
import csv


SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']

API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'CLIENT_SECRET_FILE.json'

def get_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  #builds api-specific service
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

def execute_api_request(client_library_function, **kwargs):
  response = client_library_function(
    **kwargs
  ).execute()

  print(response)

columnHeaders = []

# create a CSV output for video list    
csvFile = open('video_result.csv','w')
csvWriter = csv.writer(csvFile)
csvWriter.writerow(["views","comments","likes","estimatedMinutesWatched","averageViewDuration"])


if __name__ == '__main__':
  # Disable OAuthlib's HTTPs verification when running locally.
  # *DO NOT* leave this option enabled when running in production.
  os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

  youtubeAnalytics = get_service()

  execute_api_request(
      youtubeAnalytics.reports().query,
      ids='channel==UCU_N4jDOub9J8splDAPiMWA',

      #needs to be of form YYYY-MM-DD
      startDate='2018-01-01',
      endDate='2018-05-01',
      metrics='views,comments,likes,dislikes,estimatedMinutesWatched,averageViewDuration',
      dimensions='day',
      filters='video==ZeY6BKqIZGk,YKFWUX9w4eY,bDPdrWS-YUc'
      )

回答1:


You can see in the Reports: Query front page that you need to use the new scope:

https://www.googleapis.com/auth/youtube.readonly

instead of the old one:

https://www.googleapis.com/auth/yt-analytics.readonly 

After changing the scope, perform a re-authentication (delete the old credentials) for the new scope to take effect.

This is also confirmed in this forum.




回答2:


One of the mishaps may come if you chose wrong account/accounts during oAuth2 authorisation. For instance you may have to get "account" on the firs screen but then on second screen (during authorisation) use "brand account" and not the main account from the first step that also is on a list for second step.




回答3:


I got the same problem and replacing with https://www.googleapis.com/auth/youtube.readonly scope doesn't work. (Even making requests in the API webpage, it returns empty rows.) Instead, using the https://www.googleapis.com/auth/youtube scope works fine in my case.



来源:https://stackoverflow.com/questions/50647423/youtube-analytics-api-rows-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!