Apps 脚本不断向 Youtube API 请求离线许可
Apps Script keeps asking for offline permission with Youtube's API
我正在编写一个独立的 Google Apps 脚本,它将查询 Youtube 报告 API 和 return 我的视频分析。
当我在 Apps 脚本中使用我们用来管理各种 Youtube 频道的 Google+ 页面之一进行身份验证时,Apps 脚本一直要求我授权脚本进行离线访问。
当我使用个人 gmail 帐户在新脚本项目中尝试同样的操作时,一切正常。但是这样做有一个问题。当我通过自己的帐户查询 Youtube.Channels.list 时,我没有得到我拥有的任何其他频道的列表,只有我自己的个人频道。
我也尝试过使用 Youtube 文档中的 try it 功能和 Google+ 页面,一切正常。
对于此脚本,我在 Google 开发人员控制台中打开并启用了以下高级服务:
- Youtube 数据API
- Youtube 分析API
- Google+ API(以防万一)
我也试过专门将频道 ID 设置为 channel==xxx 并删除变量 MyChannels 和 channel,我仍然得到相同的结果。
可能的解决方案是什么?
这是我的代码示例:
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'});
}
您需要使用 OAuth 验证 Google AppScript。 IT 将为您提供在线和离线访问。
Google API 使用 OAuth 2.0 协议进行身份验证和授权。 Google 支持常见的 OAuth 2.0 场景,例如用于 Web 服务器、已安装和客户端应用程序的场景。
这里 guideline 介绍了如何将 OAuth 与您的 Apps 脚本结合使用。
在 Channel Reports documentation 中说:
the user authorizing the request must be the owner of the channel
如果频道所有者(即频道管理员)未 运行 使用您的 Apps 脚本,则会出现此问题。换句话说,如果您的 Apps 脚本是使用您的 Google 帐户编写的,该帐户也用作 YouTube 频道所有者,您就没事,否则您会遇到身份验证问题。
我记录的一个解决方案是 Using Google Apps Script to proxy YouTube Analytics Channel Reports,它提供了有关此的更多详细信息。
可能造成问题的另一个因素是您的 YouTubeAnalytics 查询缺少 start-date
和 end-date
- Report: Query documentation 将这些指示为必需参数。要解决此问题,您的函数可以重写为:
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);
}
我正在编写一个独立的 Google Apps 脚本,它将查询 Youtube 报告 API 和 return 我的视频分析。
当我在 Apps 脚本中使用我们用来管理各种 Youtube 频道的 Google+ 页面之一进行身份验证时,Apps 脚本一直要求我授权脚本进行离线访问。
当我使用个人 gmail 帐户在新脚本项目中尝试同样的操作时,一切正常。但是这样做有一个问题。当我通过自己的帐户查询 Youtube.Channels.list 时,我没有得到我拥有的任何其他频道的列表,只有我自己的个人频道。
我也尝试过使用 Youtube 文档中的 try it 功能和 Google+ 页面,一切正常。
对于此脚本,我在 Google 开发人员控制台中打开并启用了以下高级服务:
- Youtube 数据API
- Youtube 分析API
- Google+ API(以防万一)
我也试过专门将频道 ID 设置为 channel==xxx 并删除变量 MyChannels 和 channel,我仍然得到相同的结果。
可能的解决方案是什么?
这是我的代码示例:
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'});
}
您需要使用 OAuth 验证 Google AppScript。 IT 将为您提供在线和离线访问。
Google API 使用 OAuth 2.0 协议进行身份验证和授权。 Google 支持常见的 OAuth 2.0 场景,例如用于 Web 服务器、已安装和客户端应用程序的场景。
这里 guideline 介绍了如何将 OAuth 与您的 Apps 脚本结合使用。
在 Channel Reports documentation 中说:
the user authorizing the request must be the owner of the channel
如果频道所有者(即频道管理员)未 运行 使用您的 Apps 脚本,则会出现此问题。换句话说,如果您的 Apps 脚本是使用您的 Google 帐户编写的,该帐户也用作 YouTube 频道所有者,您就没事,否则您会遇到身份验证问题。
我记录的一个解决方案是 Using Google Apps Script to proxy YouTube Analytics Channel Reports,它提供了有关此的更多详细信息。
可能造成问题的另一个因素是您的 YouTubeAnalytics 查询缺少 start-date
和 end-date
- Report: Query documentation 将这些指示为必需参数。要解决此问题,您的函数可以重写为:
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);
}