0

Apps Script keeps asking for offline permission with Youtube's API

In Google Apps Script, access to YouTube Reporting API is Forbidden

These are very similar questions however, I am logged in as the owner. The script is in the owner's drive.

My problem

I have been trying to access YouTube Analytics Reports from the Content Owner using GAS(Google Apps Script). There are two accounts a Brand account (holds the data i.e. the video content) and a User account. They both use the same credentials(email & pwd). When authorizing as the user, I get a 403(forbidden). When authorizing as the brand, I am asked to authorize offline access again and again, stopping me from ever being able to make a request.

What I've tried...

  • Running from Node, using OAUTH Playground tokens, and it works. But I need to stick with GAS.
  • Using google-script-oauth2: to hard code credentials (similarly to the Node solution) but I couldn't set the refresh token. I got a 401(invalid credentials) - I believe the bearer token was expired
  • Setting up credentials for and enabling both youtube and youtube analytics APIs
  • scrapping old credentials and attaching a new project to GAS
  • removing auth in privacy settings and re-authenticating
  • toggling a use unsafe scripts option and repeating step 3
  • switching between contentOwner and channel parameters for the ids param
  • NOTE: I have two OAUTH2 credentials app script and the web client both are set as Web Applications.

GAS Code

function test(){
   YouTubeAnalytics.Reports.query("contentOwner==id", "2017-03-01", "2017-03-31", "likes");
}

Final Thoughts

I believe there is a disconnect between the Brand and the user such that the brand is not given the same access to the OAUTH 2 token. Therefore, it never generates a refresh token. So, the request sees that I am the authorized channel owner but it can't finish the OAUTH flow.

If this is true, why, and how do I fix it? If not, what do you think could be the error?

I just realized the script is in the user account and not the brand account. Would that affect the validation?

Also on the actual channel, the user is listed as the Primary Owner.

Community
  • 1
  • 1
Lea Fox
  • 76
  • 1
  • 8

1 Answers1

0

I think the problem has to do with Apps Script's limited OAuth scopes. Open the Script Editor and navigate to Resources->Advanced Google Services and you'll see that YouTube's Reporting API is not supported. However, you can get around this by using Service Accounts. They are a pain to set up but once you get them up and running you can explicitly request the scopes required to make authorized calls on the API from GAS.

Here are a few links to get you started:

Using OAuth 2.0 for Server to Server Applications

OAuth 2.0 Scopes for Google APIs

For Using OAuth 2.0 for Server to Server Applications you'll want to scroll down to the section titled Preparing to make an authorized API call. Select the HTTP/REST tab and it should walk you thru the steps you'll need to setup a service account (you'll need to create a JWT - JSON Web Token from your Google console for use in your API calls). You'll also need to retrieve the needed OAuth scope urls for YouTube's Reporting API from the second link provided. Once you're all set up you'll be able to make requests to the API directly from Apps Script using URLFetchApp. Best of luck with your project.

One more thing. Check out Google's API Explorer listing for the YouTube Reporting API. You can get the URL endpoints (and request methods) you'll need to use in your UrlFetchApp calls by playing with the Explorer.


UPDATE 4/28/2017

Also be sure to enable the YouTube Reporting API from your Google API Console.

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30