1

How do I use the 'Job' resource within the Google Apps Script?

I understand that YouTube Reporting API has to be enabled, but what object can it be called from?

I am trying to access YouTube Reporting API. is this the right way to call it? If so, my Apps Script can't identify youtubeReporting, why would that be?

youtubeReporting.jobs()

To sum up, my goal is to extract 'Content Owner Reports' bulk/query, pretty much read all the steps leading to that, except the very starting point, which is an object name.

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14
Eslam Rady
  • 45
  • 6

2 Answers2

1

Note: Although this answer deals with YouTube reporting api, the flow should be the same for all Google apis, where advanced Google service wrapper isn't provided.

AFAICT, YouTube reporting API isn't directly available as a advanced Google service. You may be able to use YouTubeanalytics api, which is provided as a advanced Google service. To access reporting api, You need to directly connect to the api through HTTP calls and .

Required reading:

Solution:

  • It is possible to access YouTube reporting api from Google apps script using UrlFetchApp
  • Full OAuth flow can be bypassed using oauth token provided by ScriptApp
  • Include scopes in the appsscript.json manifest file.
  • Switch to a standard GCP and enable the YouTube reporting api for this project. Otherwise 403 will be returned.

Snippet:

/**
 * @description A wrapper for accessing Google apis from  apps script
 * @param {string} url Google URL endpoint
 * @param {string} method HTTP method
 * @param {object} requestBody Requestbody to send
 * @param {object} pathParameters Parameters to modify in the url
 * @param {object} queryParameters Queryparameters to append to the url
 * @return {object} response
 */
function accessGoogleApiHTTP_(
  url,
  method,
  requestBody = {},
  pathParameters = {},
  queryParameters = {}
) {
  const modifiedUrl = Object.entries(pathParameters).reduce(
    (acc, [key, value]) => acc.replace(key, value),
    url
  );
  const queryUrl = Object.entries(queryParameters).reduce(
    (acc, param) => acc + param.map(e => encodeURIComponent(e)).join('='),
    '?'
  );
  const options = {
    method,
    contentType: 'application/json',
    headers: {
      Authorization: `Bearer ${ScriptApp.getOAuthToken()}` /*Need to set explicit scopes*/,
    },
    muteHttpExceptions: true,
    payload: JSON.stringify(requestBody),
  };
  const res = UrlFetchApp.fetch(
    modifiedUrl + queryUrl,
    options
  ).getContentText();
  console.log(res);
  return JSON.parse(res);
}
/**
 * @see https://developers.google.com/youtube/reporting/v1/reference/rest/v1/jobs/create
 * @description POST https://youtubereporting.googleapis.com/v1/jobs
 */
function createYtReportingJob() {
  const reportTypeId = 'id',
    name = 'name';
  const jsonRes = accessGoogleApiHTTP_(
    'https://youtubereporting.googleapis.com/v1/jobs',
    'POST',
    { reportTypeId, name },
    undefined,
    { onBehalfOfContentOwner: 'contentOwnerId' }
  );
}

Manifest scopes:

"oauthScopes": [
  "https://www.googleapis.com/auth/yt-analytics",
  "https://www.googleapis.com/auth/script.external_request"
]
TheMaster
  • 45,448
  • 6
  • 62
  • 85
0

Answer

In order to access the YouTube Reporting API you have to add YouTube Analytics API and optionally add the YouTube Data API as well. From this point you can create a report as in the Apps Script: YouTube Analytics example.

Bear in mind:

  • When importing the services like YouTube Analytics API be aware with the version.

  • You can specify the amount of metrics desired.

Reference

Apps Script: YouTube Analytics

YouTube Data API: Channel list

Analytics and Reporting APIs: Content Owner Reports

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14