本文介绍了YouTube Apps脚本API只能由没有YouTube频道的帐号运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每当我尝试执行一个需要授权访问API的脚本时, ,如果我选择从GMail帐户授权,而代码不是YouTube帐户,则代码只会在授权后继续执行。

在选择从YouTube帐户授权时,代码从字面上看并不会执行。即使在函数第1行上的Logger.Log()调用也不会触发,除非代码仅由GMail帐户授权。



YouTube帐户,它会持续循环并永久请求授权而不会继续。当从GMail帐户授权时,代码无法正常工作,因为它正在请求YouTube数据。



当然问题在于我试图访问我的YouTube帐户中的分析数据,这是GMail帐户所没有的。

真的希望有人能够提供有关这方面的见解。我已经在多个浏览器,不同的YouTube帐户,GMails等中进行了测试,问题依然存在。



任何需要YouTube Analytics API的代码都会发生这种情况。可复制在这里找到了示例代码,粘贴如下:

 功能spreadsheetAnalytics (){
//获取频道ID
var myChannels = YouTube.Channels.list('id',{mine:true});
var channel = myChannels.items [0];
var channelId = channel.id;

//设置我们报告的日期
var today = new Date();
var oneMonthAgo = new Date();
oneMonthAgo.setMonth(today.getMonth() - 1);
变种todayFormatted = Utilities.formatDate(今天, 'UTC', 'YYYY-MM-DD')
变种oneMonthAgoFormatted = Utilities.formatDate(oneMonthAgo, 'UTC', 'YYYY-MM-DD') ;

// YouTubeAnalytics.Reports.query()函数有四个必需的参数和一个可选的
//参数。第一个参数标识您是
//检索数据的频道或内容所有者。第二个和第三个参数分别指定
//报告的开始日期和结束日期。第四个参数标识您正在检索的度量标准。
//第五个参数是一个对象,它包含您要设置的任何其他可选参数
//(尺寸,过滤器,排序等)。
变种analyticsResponse = YouTubeAnalytics.Reports.query(
'信道==' +的channelID,
oneMonthAgoFormatted,
todayFormatted,
'的观点,喜好,厌恶,股' ,
{
维度:'day',
sort:'-day'
});

//创建一个包含与我们日期相对应的行和列的新电子表格
var ssName ='YouTube频道报告'+ oneMonthAgoFormatted +' - '+ todayFormatted;
var numRows = analyticsResponse.rows.length;
var numCols = analyticsResponse.columnHeaders.length;

//为列标题添加额外的行
var ssNew = SpreadsheetApp.create(ssName,numRows + 1,numCols);

//获取第一张图片
var sheet = ssNew.getSheets()[0];

//获取标题列的范围
//记住,电子表格是1索引的,而数组是0索引的
var headersRange = sheet.getRange(1, 1,1,numCols);
var headers = [];

//这些列标题将与初始调用中所请求的度量标准
//相对应:views,likes,dislikes,分享
(analyticsResponse.columnHeaders中的var i) {
var columnHeader = analyticsResponse.columnHeaders [i];
var columnName = columnHeader.name;
标题[i] = columnName;
}
//这需要一个2维数组
headersRange.setValues([headers]);

//加粗并冻结列名
headersRange.setFontWeight('bold');
sheet.setFrozenRows(1);

//获取数据范围并设置值
var dataRange = sheet.getRange(2,1,numRows,numCols);
dataRange.setValues(analyticsResponse.rows);

//加粗和冻结日期
var dateHeaders = sheet.getRange(1,1,numRows,1);
dateHeaders.setFontWeight('bold');
sheet.setFrozenColumns(1);

//在我们的范围内包含标题。标题用于
//标记轴
var range = sheet.getRange(1,1,numRows,numCols);
var chart = sheet.newChart()
.asColumnChart()
.setStacked()
.addRange(范围)
.setPosition(4,2,10, 10)
.build();
sheet.insertChart(图表);

}
youtube.gs


解决方案 div>

有一个,Google标记为不会修正',其中描述了您的问题:

Google确实有:

由于这是我遇到的问题,因此我。由于您需要的代码也需要通过YouTube Analytics API访问,因此需要在下面详细介绍一些额外的设置,并在教程中引用该部分:


  1. 添加YouTube分析库ID 1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL (1.4部分)

  2. 添加附加的范围为 getYouTubeService()用于 yt-analytics-monetary.readonly yt-analytics.readonly (第2.2部分)

      //设置范围和Google特定的其他参数。 
    .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])


  3. 启用YouTube Analytics API以及YouTube数据API(第3.4部分)



  4. 代码示例中还有一些小的修改以使库自动完成,例如 YouTube.Channels.list()变成 YouTube.channelsList() YouTubeAnalytics.Reports。 query()变成 YouTubeAnalytics.reportsQuery()



    在。您仍然需要完成本教程中概述的控制台项目以及 auth.gs 表单中的所有设置(我已将该示例修改为在 @OnlyCurrentDoc 以避免应用验证)。注意 :当您运行 logRedirectUri()时,您需要使用Google云端硬盘帐户进行身份验证,并且在新浏览器标签中复制身份验证网址时,请选择您需要数据的YouTube帐户。

    This is such a strange issue and there seems to be no documentation of it anywhere online.

    Whenever I try to execute a script that requires authorization to access an API, the code will only continue executing after authorization if I choose to authorize it from a GMail account, but not a YouTube account.

    The code literally does not execute after choosing to authorize it from a YouTube account. Even a Logger.Log() call on Line 1 of the function will not trigger unless the code is authorized from a GMail account only.

    When authorizing from a YouTube account, it will continually loop and request authorization forever without continuing. When authorizing from a GMail account, the code doesn't work because it is requesting YouTube data.

    The problem of course is that I'm trying to access Analytics data from my YouTube account, which the GMail account does not have.

    Really hoping someone is able to provide insight into this. I've tested in multiple browsers, different YouTube accounts, GMails, etc. and the issue persists.

    It also happens with any code that requests YouTube's Analytics API. Replicable with the sample code found here, pasted below: 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
    
    解决方案

    There is a related issue ticket which Google have marked as 'Wont't fix' which describes your problem:

    Google do have a suggested workaround:

    As this is a problem I've also encountered I've published a detailed tutorial that uses the YouTube Data API. As the code you require also needs access via to the YouTube Analytics API there is some additional setup required detailed below and referencing the part in the tutorial:

    1. Add a YouTube Analytics library with id 1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL (Part 1.4)
    2. Add additional scopes to getYouTubeService() for yt-analytics-monetary.readonly and yt-analytics.readonly (Part 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"])
      

    3. Enabling the YouTube Analytics API as well as the YouTube Data API (Part 3.4)

    There is also some minor modification in your code sample as to enable the library autocomplete e.g. YouTube.Channels.list() becomes YouTube.channelsList() and YouTubeAnalytics.Reports.query() becomes YouTubeAnalytics.reportsQuery()

    All these changes have been included in this example sheet. You'll still need to do all the setup of the console project outlined in the tutorial and in the auth.gs sheet (I've modified the example to run in @OnlyCurrentDoc to avoid app verification).

    Note: when you run logRedirectUri() you need to authenticate with your Google Drive account and when copy the authentication url in a new browser tab select the YouTube account you want data for.

    这篇关于YouTube Apps脚本API只能由没有YouTube频道的帐号运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:03