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

北战南征 提交于 2020-01-03 02:12:07

问题


I'm writing a standalone Google Apps Script that will query the Youtube Reports API and return analytics on my videos.

When I authenticate in Apps Script with one of the Google+ pages that we use to manage our various Youtube Channels, Apps Script keeps asking me to authorize the script for offline access.

When I try the same thing in a new script project with my personal gmail account, everything works fine. There's a problem with doing it this way though. When I query the Youtube.Channels.list through my own account, I don't get a list of any of the other channels I own, just my own personal channel.

I've also tried this with the try it feature on the Youtube documentation with a Google+ page and things work fine.

For this script, I have the following advanced services turned on & enabled in the Google Developer Console:

  1. Youtube Data API
  2. Youtube Analytics API
  3. Google+ API (just in case)

I've also tried specifically setting the channel id to channel==xxx and removing the varibles MyChannels and channel and I still get the same results.

What could be a possible solution?

Here's an example of my code:

function youTubeAnalytics() {
     var myChannels = YouTube.Channels.list('id', {mine: true});
     var channel = myChannels.items[0];
     var channelId = channel.id;
     var analyticsResponse = YouTubeAnalytics.Reports.query(
     'channel==' + channelId,
     'estimatedMinutesWatched,views,likes,subscribersGained',
     {dimensions: 'video','max-results': '200',sort: '-views'});
     }

回答1:


In the Channel Reports documentation it says:

the user authorizing the request must be the owner of the channel

This issue surfaces if your Apps Script isn't been run by the channel owner ie a channel manager. In other words if your Apps Script is written using your Google account which is also used as the YouTube Channel owner you are fine, otherwise you hit authentication issues.

A solution I've documented is Using Google Apps Script to proxy YouTube Analytics Channel Reports which gives more detail about this.

Another factor that might be creating issues is your YouTubeAnalytics query is missing start-date and end-date - the Report: Query documentation indicates these as required parameters. To fix this your function could be rewritten as:

function youTubeAnalytics() {
  var myChannels = YouTube.Channels.list('id', {mine: true});
  var channel = myChannels.items[0];
  var channelId = channel.id;
  var analyticsResponse = YouTubeAnalytics.Reports.query(
  'channel==' + channelId, 
  '2010-01-01', 
  Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd'), 
  'estimatedMinutesWatched,views,likes,subscribersGained', 
  {dimensions: 'video','max-results': '200',sort: '-views'});
  Logger.log(analyticsResponse);
}



回答2:


You need to authenticate the Google AppScript with OAuth. IT will provide you both online and offline access.

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, installed, and client-side applications.

Here is some guideline on how to use OAuth with your Apps Script.



来源:https://stackoverflow.com/questions/36485973/apps-script-keeps-asking-for-offline-permission-with-youtubes-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!