YouTube分析API行为空 [英] YouTube analytics API rows empty

查看:126
本文介绍了YouTube分析API行为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前已经回答过,但是我似乎有一个不同的问题.直到几天前,我对YouTube的查询都没有出现问题.但是,现在,每当我在任何视频上查询数据时,实际视频数据的行就会作为一个空数组返回.

这是我的完整代码:

# -*- 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'
      )

解决方案

您可以在报告:查询首页中看到以下内容:您需要使用新的作用域:

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

代替旧的:

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

更改范围后,请重新进行身份验证(删除旧凭据),以使新范围生效.

这也在此论坛中得到确认./p>

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'
      )

解决方案

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.

这篇关于YouTube分析API行为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆