0

This code works fine when I'm accessing my own channel:

function test(){
  const channelId = "UC702oPAKxKSQdxvqnAWlkiA"

  let response = YouTube.Channels.list('statistics',{id:channelId});
  let stats = response.items[0].statistics;
  Logger.log(stats);

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

  const out = [["Subscriber Count","30 Day Views","Total Views"],
              [stats.subscriberCount,result.rows[0][0],stats.viewCount]];
}

However, I need to use this on accounts for my various clients. When I change the channelId to the ID of the other channel I get "GoogleJsonResponseException: API call to youtubeAnalytics.reports.query failed with error: Forbidden." The YouTube API doesn't have any problem, but it doesn't give me any ability to check the 30-day values, only the total values. I've looked at the OAuth2 instructions and frankly I'm lost there. Can this code be made to work for other channels? Can I get OAuth2 working for any channel? Can I get OAuth2 working for a specific set of channels if the client can do something? Or is this basically impossible?

Spencer
  • 453
  • 4
  • 21

0 Answers0