Google apps script YouTube应用程序脚本API只能由没有YouTube频道的帐户运行?
这是一个如此奇怪的问题,而且似乎没有任何在线文档 每当我尝试执行一个需要授权才能访问API的脚本时,如果我选择从GMail帐户(而不是YouTube帐户)授权,则代码只有在授权后才会继续执行 从YouTube帐户中选择授权后,代码实际上不会执行。即使是函数第1行的Logger.Log()调用也不会触发,除非代码仅从GMail帐户授权 当从YouTube帐户授权时,它将持续循环并请求授权,而不会继续。当从GMail帐户授权时,代码不起作用,因为它正在请求YouTube数据。 当然,问题是我试图从我的YouTube帐户访问分析数据,而GMail帐户没有 真的希望有人能提供这方面的见解。我已经在多个浏览器、不同的YouTube帐户、Gmail等上进行了测试,但问题依然存在 任何请求YouTube分析API的代码都会出现这种情况。可使用此处找到的示例代码复制,粘贴在下面:Google apps script YouTube应用程序脚本API只能由没有YouTube频道的帐户运行?,google-apps-script,google-apps,Google Apps Script,Google Apps,这是一个如此奇怪的问题,而且似乎没有任何在线文档 每当我尝试执行一个需要授权才能访问API的脚本时,如果我选择从GMail帐户(而不是YouTube帐户)授权,则代码只有在授权后才会继续执行 从YouTube帐户中选择授权后,代码实际上不会执行。即使是函数第1行的Logger.Log()调用也不会触发,除非代码仅从GMail帐户授权 当从YouTube帐户授权时,它将持续循环并请求授权,而不会继续。当从GMail帐户授权时,代码不起作用,因为它正在请求YouTube数据。 当然,问题是我试图从我
请确保为此使用了正确的作用域。要检索有关已为频道或内容所有者安排的特定报告作业的信息,必须使用以下命令:
https://www.googleapis.com/auth/yt-analytics.readonly
- 查看YouTube内容的YouTube分析报告。此范围提供对用户活动度量的访问,如查看计数和评级计数
https://www.googleapis.com/auth/yt-analytics-monetary.readonly
- 查看YouTube内容的YouTube分析报告。此范围提供对用户活动指标、估计收入和广告绩效指标的访问
1MWD64g7dq\u ZhlN8HU\u O6BRu5xNwywhp8V76utKowZEtcirEgO3t\u JFFL的YouTube分析库(第1.4部分)
yt analytics monetary.readonly
和yt analytics.readonly添加其他作用域到GetYouTube服务()
(第2.2部分)
YouTube.Channels.list()
变成YouTube.channelsList()
和YouTubeAnalytics.Reports.query()
变成YouTubeAnalytics.reportsQuery()
所有这些更改都已包含在中。您仍然需要完成教程和auth.gs
工作表中概述的控制台项目的所有设置(为了避免应用程序验证,我已将示例修改为在@OnlyCurrentDoc
中运行)
注意:当您运行
logRedirectUri()
时,您需要使用您的Google Drive帐户进行身份验证,当您在新的浏览器选项卡中复制身份验证url时,请选择您想要获取数据的YouTube帐户 这对我起了作用。非常感谢你的帮助,你是个英雄!我不明白。为什么我们需要使用您正在显示的YouTube分析库,而不是Google Advanced Services中支持的YouTube分析API?我完全遵循了你的教程,最终能够连接到正确的YouTube帐户(品牌),但无法查询任何内容,因为它与YouTube官方API文档的语法不同。例如:您有var myChannels=YouTube.channelsList('id',{mine:true})
而YouTube文档显示YouTube.channels.list()
。所以我很难构建这个库,因为我对YT的API很陌生。
function spreadsheetAnalytics() {
// Get the channel ID
var myChannels = YouTube.Channels.list('id', {mine: true});
var channel = myChannels.items[0];
var channelId = channel.id;
// Set the dates for our report
var today = new Date();
var oneMonthAgo = new Date();
oneMonthAgo.setMonth(today.getMonth() - 1);
var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')
var oneMonthAgoFormatted = Utilities.formatDate(oneMonthAgo, 'UTC', 'yyyy-MM-dd');
// The YouTubeAnalytics.Reports.query() function has four required parameters and one optional
// parameter. The first parameter identifies the channel or content owner for which you are
// retrieving data. The second and third parameters specify the start and end dates for the
// report, respectively. The fourth parameter identifies the metrics that you are retrieving.
// The fifth parameter is an object that contains any additional optional parameters
// (dimensions, filters, sort, etc.) that you want to set.
var analyticsResponse = YouTubeAnalytics.Reports.query(
'channel==' + channelId,
oneMonthAgoFormatted,
todayFormatted,
'views,likes,dislikes,shares',
{
dimensions: 'day',
sort: '-day'
});
// Create a new Spreadsheet with rows and columns corresponding to our dates
var ssName = 'YouTube channel report ' + oneMonthAgoFormatted + ' - ' + todayFormatted;
var numRows = analyticsResponse.rows.length;
var numCols = analyticsResponse.columnHeaders.length;
// Add an extra row for column headers
var ssNew = SpreadsheetApp.create(ssName, numRows + 1, numCols);
// Get the first sheet
var sheet = ssNew.getSheets()[0];
// Get the range for the title columns
// Remember, spreadsheets are 1-indexed, whereas arrays are 0-indexed
var headersRange = sheet.getRange(1, 1, 1, numCols);
var headers = [];
// These column headers will correspond with the metrics requested
// in the initial call: views, likes, dislikes, shares
for(var i in analyticsResponse.columnHeaders) {
var columnHeader = analyticsResponse.columnHeaders[i];
var columnName = columnHeader.name;
headers[i] = columnName;
}
// This takes a 2 dimensional array
headersRange.setValues([headers]);
// Bold and freeze the column names
headersRange.setFontWeight('bold');
sheet.setFrozenRows(1);
// Get the data range and set the values
var dataRange = sheet.getRange(2, 1, numRows, numCols);
dataRange.setValues(analyticsResponse.rows);
// Bold and freeze the dates
var dateHeaders = sheet.getRange(1, 1, numRows, 1);
dateHeaders.setFontWeight('bold');
sheet.setFrozenColumns(1);
// Include the headers in our range. The headers are used
// to label the axes
var range = sheet.getRange(1, 1, numRows, numCols);
var chart = sheet.newChart()
.asColumnChart()
.setStacked()
.addRange(range)
.setPosition(4, 2, 10, 10)
.build();
sheet.insertChart(chart);
}
youtube.gs
// Set the scope and additional Google-specific parameters.
.setScope(["https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtube.readonly",
"https://www.googleapis.com/auth/youtubepartner",
"https://www.googleapis.com/auth/youtubepartner-channel-audit",
"https://www.googleapis.com/auth/yt-analytics-monetary.readonly",
"https://www.googleapis.com/auth/yt-analytics.readonly"])