Trouble Retrieving YouTube Channel Report with Google Analytics API: 403 Forbidden Error
12:37 07 Sep 2024

I want to generate a report for a YouTube channel for which I have administrator access, using the Google Analytics API. (I'm not channel owner.)

However, when executing the following code in GAS, I receive a 403 Forbidden error.

const channels = YouTube.Channels.list('id,contentDetails', {
  id: "hogehoge"
});
const channelId = channels.items[0].id;

const oneMonthInMillis = 1000 * 60 * 60 * 24 * 30;
const today = new Date();
const lastMonth = new Date(today.getTime() - oneMonthInMillis);

const metrics = [
  'views',
];
const result = YouTubeAnalytics.Reports.query({
  ids: 'channel==' + channelId,
  startDate: formatDateString(lastMonth),
  endDate: formatDateString(today),
  metrics: metrics.join(','),
  dimensions: 'day',
  sort: 'day',
});

I am referring to the documentation at https://developers.google.com/apps-script/advanced/youtube-analytics?hl=ja.

How can I retrieve the report? Or is this issue related to permissions and therefore, data cannot be output?

google-apps-script youtube-api