Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Youtube Analytics API using R

I am trying to pull data from Youtube Analytics API using R and have hit a wall. I'm getting a Status 403 error. I can confirm that:

  • I have tried logging out of all accounts, rebooting, re-authenticating and then logging into 1 account and it didn't work
  • I have tried using just the https://www.googleapis.com/auth/youtube.readonly scope (which is supposed to be correct) and all youtube analytics scopes are authorized within the app (each with their own credential key), it didn't work.
  • I have tried authenticating from different browsers, it didn't work
  • I can confirm that the call worked from https://developers.google.com/apis-explorer/#p/youtubeAnalytics/v2/youtubeAnalytics.reports.query.

UPDATE I have found the solution and updated the code to what is now working

scopes<- c('https://www.googleapis.com/auth/youtube.readonly')    
endpoint<- oauth_endpoints('google')
youtubeapp<- oauth_app(appname='Youtube Analytics API',
                       key=client_id,
                       secret=client_secret)
youtube_token<- oauth2.0_token(
  endpoint= oauth_endpoints('google'),
  app = youtubeapp,
  scope = scopes)
raw_data<- GET(paste("https://youtubeanalytics.googleapis.com/v2/reports?dimensions=",dimensions,"&endDate=",enddate,"&ids=",ids,"&metrics=",metrics,"&startDate=",startdate,sep=""),
               config=youtube_token,content_type('application/json'))
content(raw_data, as= 'text')
like image 841
Duke Avatar asked Sep 05 '25 02:09

Duke


1 Answers

I have figured out that access permissions are dictated by Youtube (not API IAM), so for example even though my personal email is owner of Brand account and API I still need to authenticate the call using the Brand Account or it won't work (I get 403 error for personal email). I tried switching my Youtube channel from Brand to personal and then my personal email worked. However, I need it under the brand account, so I have switched back.

I will leave this code up regardless because I found that good examples using R were lacking, I hope you find this useful.

like image 68
Duke Avatar answered Sep 07 '25 00:09

Duke