3

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'
      )
pnuts
  • 58,317
  • 11
  • 87
  • 139

3 Answers3

2

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.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • I did it but no use. I did delete old credentials and created new and re-authenticated. Any other clues? I am getting only column headers. – Ravindra Gullapalli Sep 03 '18 at 16:34
  • 1
    thanks noogui, now it's working for me !! You can declare many scope with a array, my scope are : ` $this->client->setScopes(array( 'https://www.googleapis.com/auth/youtube.force-ssl', 'https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly', ) ); ` – tomboul Oct 24 '18 at 09:36
0

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.

smentek
  • 2,820
  • 1
  • 28
  • 32
0

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.

Bella
  • 1