YouTube Apps 脚本 API 只能由没有 YouTube 频道的帐户运行吗?
YouTube Apps Script API only runnable by accounts without a YouTube channel?
这是一个很奇怪的问题,网上似乎没有关于它的文档。
每当我尝试执行需要授权才能访问 API 的脚本时,如果我选择从 GMail 帐户而不是 YouTube 帐户授权,代码只会在授权后继续执行。
选择从 YouTube 帐户授权后,代码实际上不会执行。即使是函数第 1 行的 Logger.Log() 调用也不会触发,除非该代码仅从 GMail 帐户授权。
从 YouTube 帐户授权时,它会不断循环并永远请求授权,而不会继续。从 GMail 帐户授权时,代码不起作用,因为它正在请求 YouTube 数据。
问题当然是我试图从我的 YouTube 帐户访问分析数据,而 GMail 帐户没有。
真的希望有人能够对此提供见解。我在多个浏览器、不同的 YouTube 帐户、GMail 等中进行了测试,但问题仍然存在。
任何请求 YouTube 分析的代码都会发生这种情况 API。可复制此处找到的示例代码,粘贴在下方:https://developers.google.com/youtube/analytics/v1/code_samples/apps-script#export_youtube_analytics_data_to_google_sheets
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
确保为此使用了正确的范围。要检索有关为频道或内容所有者安排的特定报告作业的信息,您必须使用以下 authorization scopes:
https://www.googleapis.com/auth/yt-analytics.readonly
- 查看 YouTube 内容的 YouTube 分析报告。此范围提供对用户 activity 指标的访问权限,例如观看次数和评分次数。
https://www.googleapis.com/auth/yt-analytics-monetary.readonly
- 查看 YouTube 内容的 YouTube 分析货币报告。此范围提供对用户 activity 指标以及估算收入和广告效果指标的访问权限。
查看此 link 了解更多信息。
有一个 related issue ticket Google 标记为“不会修复”,它描述了您的问题:
If you are trying to use the YouTube API to get data about a YouTube Channel that you manage using the Google+ association system, it will get you stuck in an infinite loop. Google+ pages can “own” a YouTube channel, and the authorization will not work when trying to use one of those channels
Google确实有一个suggested workaround:
use the OAuth2 library to authorize access to the G+ page, and then use UrlFetchApp instead of the YouTube Advanced service to make requests to the API
因为这是我也遇到过的问题,所以我 published a detailed tutorial that uses the YouTube Data API。由于您需要的代码还需要通过 YouTube Analytics API 进行访问,因此需要进行一些额外的设置,详见下文并参考教程中的部分:
- 添加 ID 为
1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL
的 YouTube 分析库(第 1.4 部分)
为 yt-analytics-monetary.readonly
和 yt-analytics.readonly
添加额外的范围到 getYouTubeService()
(第 2.2 部分)
// 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"])
启用 YouTube 分析 API 以及 YouTube 数据 API(第 3.4 部分)
您的代码示例中还有一些小修改,以启用库自动完成功能,例如YouTube.Channels.list()
变为 YouTube.channelsList()
并且 YouTubeAnalytics.Reports.query()
变为 YouTubeAnalytics.reportsQuery()
所有这些更改都已包含在 this example sheet 中。您仍然需要完成本教程和 auth.gs
sheet 中概述的控制台项目的所有设置(我已将 @OnlyCurrentDoc
中的示例修改为 运行以避免应用程序验证)。
注意:当您运行logRedirectUri()
时,您需要使用您的Google进行身份验证驱动帐户并在新的浏览器选项卡中复制身份验证 url select 您想要数据的 YouTube 帐户。
这是一个很奇怪的问题,网上似乎没有关于它的文档。
每当我尝试执行需要授权才能访问 API 的脚本时,如果我选择从 GMail 帐户而不是 YouTube 帐户授权,代码只会在授权后继续执行。
选择从 YouTube 帐户授权后,代码实际上不会执行。即使是函数第 1 行的 Logger.Log() 调用也不会触发,除非该代码仅从 GMail 帐户授权。
从 YouTube 帐户授权时,它会不断循环并永远请求授权,而不会继续。从 GMail 帐户授权时,代码不起作用,因为它正在请求 YouTube 数据。
问题当然是我试图从我的 YouTube 帐户访问分析数据,而 GMail 帐户没有。
真的希望有人能够对此提供见解。我在多个浏览器、不同的 YouTube 帐户、GMail 等中进行了测试,但问题仍然存在。
任何请求 YouTube 分析的代码都会发生这种情况 API。可复制此处找到的示例代码,粘贴在下方:https://developers.google.com/youtube/analytics/v1/code_samples/apps-script#export_youtube_analytics_data_to_google_sheets
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
确保为此使用了正确的范围。要检索有关为频道或内容所有者安排的特定报告作业的信息,您必须使用以下 authorization scopes:
https://www.googleapis.com/auth/yt-analytics.readonly
- 查看 YouTube 内容的 YouTube 分析报告。此范围提供对用户 activity 指标的访问权限,例如观看次数和评分次数。
https://www.googleapis.com/auth/yt-analytics-monetary.readonly
- 查看 YouTube 内容的 YouTube 分析货币报告。此范围提供对用户 activity 指标以及估算收入和广告效果指标的访问权限。
查看此 link 了解更多信息。
有一个 related issue ticket Google 标记为“不会修复”,它描述了您的问题:
If you are trying to use the YouTube API to get data about a YouTube Channel that you manage using the Google+ association system, it will get you stuck in an infinite loop. Google+ pages can “own” a YouTube channel, and the authorization will not work when trying to use one of those channels
Google确实有一个suggested workaround:
use the OAuth2 library to authorize access to the G+ page, and then use UrlFetchApp instead of the YouTube Advanced service to make requests to the API
因为这是我也遇到过的问题,所以我 published a detailed tutorial that uses the YouTube Data API。由于您需要的代码还需要通过 YouTube Analytics API 进行访问,因此需要进行一些额外的设置,详见下文并参考教程中的部分:
- 添加 ID 为
1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL
的 YouTube 分析库(第 1.4 部分) 为
yt-analytics-monetary.readonly
和yt-analytics.readonly
添加额外的范围到getYouTubeService()
(第 2.2 部分)// 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"])
启用 YouTube 分析 API 以及 YouTube 数据 API(第 3.4 部分)
您的代码示例中还有一些小修改,以启用库自动完成功能,例如YouTube.Channels.list()
变为 YouTube.channelsList()
并且 YouTubeAnalytics.Reports.query()
变为 YouTubeAnalytics.reportsQuery()
所有这些更改都已包含在 this example sheet 中。您仍然需要完成本教程和 auth.gs
sheet 中概述的控制台项目的所有设置(我已将 @OnlyCurrentDoc
中的示例修改为 运行以避免应用程序验证)。
注意:当您运行logRedirectUri()
时,您需要使用您的Google进行身份验证驱动帐户并在新的浏览器选项卡中复制身份验证 url select 您想要数据的 YouTube 帐户。