Search code examples
rhttrgoogle-apis-exploreryoutube-analytics-api

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:

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

Solution

  • 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.