0

I'm trying to use the YouTube Analytics and Reporting API with Google Apps Script to populate a Google Sheet with analytics data from YouTube brand accounts that I own and manage.

As a preface, I have tried this on YouTube channels that I own and channels that I manage and nothing has worked. I have also taken a look at all of the Stack Overflow questions & responses related to this for the past few days and nothing has worked. Believe me when I say I have tried everything. I do not believe the code or the way I set things up is the issue; I believe it has to do with Google or the YouTube API itself.

Here are the steps I have taken:

  • Enable the YouTube Analytics API and YouTube Data API in Google Apps Script. (The YouTube Reporting API is nowhere to be found which may be the issue...)

  • Enable the same APIs in Google Developer Console (The YouTube Reporting API is available there)

  • Set up a Client ID & Secret and linked my project to the Apps Script

  • Verified that my code indeed works by inputting the same exact parameters (metrics, dimensions, etc.) in the YouTube Analytics and Reporting API Explorer in Google Developer Console. When I do this, the explorer does indeed export the right information. However, in Google Apps Script, it does not work at all.

Due to the fact that the code works in the Explorer, but not in Google Apps Script, I believe that there's something simple that I'm doing wrong, or it's an error on the Google side. I'm hoping that there's a way around this or a fix, because I'd really like to get the data from my YouTube channels into Google Sheets.

Google Apps Script code (shortened for simplicity. The "key" property may not be necessary, but it doesn't work without it either).

function myFunction() {
  var metrics = [
    'averageViewDuration'
  ];
  var oneMonthInMillis = 1000 * 60 * 60 * 24 * 30;
  var today = new Date();
  var lastMonth = new Date(today.getTime() - oneMonthInMillis);
  var report = YouTubeAnalytics.Reports.query({
    ids: 'channel==MINE',
    startDate: formatDateString(lastMonth),
    endDate: formatDateString(today),
    metrics: metrics.join(','),
    dimensions: 'day',
    sort: 'day',
    includeHistoricalChannelData: false,
    key: "AIzaSyAcVb-hriIydRs8iVqFZ6ZoyL8En3qLcnc",
  });

  Logger.log(report);
}

When I run the code, there is zero output to the logger. I expect there to be something there.

As a reminder, this happens when I select any account besides my core account. When I select the core account attached to the account (that does not have a YouTube channel), it does output this:

"[19-10-30 17:58:19:251 PDT] {columnHeaders=[{columnType=DIMENSION, dataType=STRING, name=day}, {columnType=METRIC, dataType=INTEGER, name=averageViewDuration}], kind=youtubeAnalytics#resultTable, rows=[[2019-09-30, 0], [2019-10-01, 0], [2019-10-02, 0], [2019-10-03, 0], [2019-10-04, 0], [2019-10-05, 0], [2019-10-06, 0], [2019-10-07, 0], [2019-10-08, 0]...]]}"

There's no reason that it shouldn't be working. What do you think is the issue?

1 Answers1

0

Sorry, but currently what you would want to do, it's not supported by Apps Script. It was already reported in Google Issue tracker, as you can see in the next links:

As a workaround, you can implement Oauth2.0 as it is said in this Stack Overflow's post

Edit

If you are having troubles using the refresh token, Use this code to make the request directly to the Youtube API after obtaining an access token from Oauth2.0 playground

function makeRequest() {
  var response = UrlFetchApp.fetch('https://youtubeanalytics.googleapis.com/v2/reports?endDate=2019-10-10&ids=channel%3D%3DMINE&metrics=views&startDate=2019-10-09', {
    headers: {
       // you will get the access token from Oauth2.0 playground
      Authorization: 'Bearer ACCES_TOKEN'   
    }
  });
  Logger.log(response)
}

From The YouTube API Will Not Authorize a Google+ Page's YouTube Channel, it says

The engineering team has decided that it's not possible to support this use case. You'll need to use something other than the YouTube advanced service to access a channel owned by a G+ page.

So, it seems there is no support for what you want

Community
  • 1
  • 1
alberto vielma
  • 2,302
  • 2
  • 8
  • 15
  • Thanks, Alberto. I've tried the steps in the post you mentioned, but I am unable to figure out how to get it to work. I've got to step 9 and generated a refresh token, but I don't know how to use that within Apps Script. Does this solve the problem of getting it to work properly within Apps Script or is this a non-Apps Script workaround? I don't know what to do next after I've generated the refresh token. – Alex Negrete Oct 31 '19 at 21:43
  • Ok, I edited my answer so you can try using an access token directly and retrieve the data you need and what I am giving you is an Apps Script workaround to do what you want – alberto vielma Nov 01 '19 at 12:01
  • Thanks for following up. I tried your code but I am getting the same result: "No logs found." When I run your code and then select the master email account (masteraccount@gmail.com) the code works correctly, however, when I run the code and then select the YouTube Channel/Brand Account that I want, nothing happens. Am I doing something wrong or is this a bigger issue? – Alex Negrete Nov 04 '19 at 20:28
  • It seems like it is not supported as I stated in my edited answer – alberto vielma Nov 06 '19 at 10:39
  • After I run the function makrequest(), I was asked to give permissions. I chose my YT brand account. Then in the log it says "This project requires access to your Google Account to run. Please try again and allow it this time." I repeat and it is the same error. FYI @AlexNegrete – Freelensia Sep 13 '21 at 07:36