Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube analytics API rows empty

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'
      )
like image 708
Matthew Dickens Avatar asked Jul 31 '26 11:07

Matthew Dickens


1 Answers

enter image description here

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.

like image 81
ReyAnthonyRenacia Avatar answered Aug 02 '26 23:08

ReyAnthonyRenacia