在App脚本(js)中运行Youtube数据服务时出错–超出了未经身份验证的使用的每日限制 [英] Error when running Youtube Data Service in App Scripts (js) – Daily Limit for Unauthenticated Use Exceeded

查看:89
本文介绍了在App脚本(js)中运行Youtube数据服务时出错–超出了未经身份验证的使用的每日限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在App脚本中运行自定义功能,该功能利用了YouTube(YouTube数据API v3)高级服务.运行时,出现以下错误:

I'm running a custom function in App Scripts which utilizes the Youtube (YouTube Data API v3) advanced service. When running, I get the following error:

GoogleJsonResponseException:对youtube.videos.list的API调用失败,并出现以下错误:超出了未经身份验证的使用的每日限制.继续使用需要注册.(第15行).

GoogleJsonResponseException: API call to youtube.videos.list failed with error: Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup. (line 15).

我不确定如何验证我的应用程序.我已将其添加到云项目中并启用了API.

I'm not sure how to authenticate my application. I've added it to a cloud project and enabled the API's.

更新:这是我的代码:

function getYoutubeData(youtubeId) {

  // Don't run on empty
  if(!youtubeId){return null}

  // Make the request
  var vidData = YouTube.Videos.list("statistics, snippet", {id: youtubeId}).items;
  if (!vidData|vidData.length<1){return null}

  // Get the first item
  vidData = vidData[0];

  return vidData.statistics
}

推荐答案

我相信您的目标如下.

  • 您要在脚本中将 vidData.statistics 的值放入单元格.
  • 您想使用 = getYoutubeData(youtubeId)之类的自定义功能来实现这一目标.
  • You want to put the value of vidData.statistics in your script to the cell.
  • You want to achieve this using custom function like =getYoutubeData(youtubeId).

为此,这个答案如何?

不幸的是,在自定义功能中使用了高级Google服务的YouTube数据API时,未使用访问令牌.从您的脚本来看,我认为您遇到问题的原因是这样的.例如,当 const sample =()=>的函数时,ScriptApp.getOAuthToken(); 用作自定义函数,如 = sample(),不返回任何值.由于安全性,我认为这是Google方面的当前规范.

Unfortunately, when YouTube Data API of Advanced Google services is used in the custom function, the access token is not used. From your script, I think that the reason of your issue is this. For example, when the function of const sample = () => ScriptApp.getOAuthToken(); is used as the custom function like =sample(), no value is returned. I think that this is the current specification of Google side because of the security.

为了在上述情况下实现目标,以下解决方法如何?

In order to achieve your goal under above situation, how about the following workarounds?

在此替代方法中,首先,将youtube ID设置为Google Spreadsheet中的单元格.而 vidData.statistics 的值是通过Google Apps脚本(不是自定义函数)检索的,并将youtube ID替换为结果值.

In this workaround, at first, the youtube ID is set to the cells in Google Spreadsheet. And the value of vidData.statistics are retrieved by the Google Apps Script which is not the custom function and replace the youtube ID with the result values.

请将youtube ID的单元格范围设置为 sourceRange 和工作表名称.在示例中,它假定将youtube ID放在单元格"A1:A10"中.并且请在脚本编辑器中运行 getYoutubeData().当然,您也可以将其设置为自定义菜单.

Please set the range of cells of youtube IDs to sourceRange and the sheet name. At the sample, it supposes that the youtube IDs are put to the cells "A1:A10". And please run getYoutubeData() at the script editor. Of course, you can also set this to the custom menu.

function getYoutubeData() {
  const sourceRange = "A1:A10"; // Please set the range of cells of youtube IDs.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");  // Please set the sheet name.

  const range = sheet.getRange(sourceRange);
  const youtubeIds = range.getValues();
  const values = youtubeIds.map(([youtubeId]) => {

    // This is your script.
    if(!youtubeId){return [null]}
    var vidData = YouTube.Videos.list("statistics, snippet", {id: youtubeId}).items;
    if (!vidData|vidData.length<1){return [null]}
    vidData = vidData[0];
    return [JSON.stringify(vidData.statistics)];

  });
  range.setValues(values);
}

解决方法2:

在此替代方法中,将使用自定义功能.但是,在这种情况下,Web Apps被用作包装器.这样,授权过程就可以在Web Apps上完成.因此,自定义功能可以在未经授权的情况下运行.请执行以下流程.

Workaround 2:

In this workaround, the custom function is used. But, in this case, the Web Apps is used as the wrapper. By this, the authorization process is done at the Web Apps. So the custom function can be run without the authorization. Please do the following flow.

使用脚本时,它如下所示.请复制以下脚本并将其粘贴到脚本编辑器中.

When your script is used, it becomes as follows. Please copy and paste the following script to the script editor.

// This is your script.
function getYoutubeData_forWebApps(youtubeId) {
  // Don't run on empty
  if(!youtubeId){return null}

  // Make the request
  var vidData = YouTube.Videos.list("statistics, snippet", {id: youtubeId}).items;
  if (!vidData|vidData.length<1){return null}

  // Get the first item
  vidData = vidData[0];

  return vidData.statistics
}

// Web Apps using as the wrapper.
function doGet(e) {
  const res = getYoutubeData_forWebApps(e.parameter.youtubeId)
  return ContentService.createTextOutput(JSON.stringify(res));
}

// This is used as the custom function.
function getYoutubeData(youtubeId) {
  const url = "https://script.google.com/macros/s/###/exec?youtubeId=" + youtubeId;  // Please set the URL of Web Apps after you set the Web Apps.
  return UrlFetchApp.fetch(url).getContentText();
}

2.部署Web应用.

  1. 在脚本编辑器上,通过发布"->作为Web应用程序部署"打开一个对话框.
  2. 以以下方式执行应用":选择我" .
    • 通过这种方式,脚本以所有者身份运行.
  • 在这种情况下,不需要请求访问令牌.我认为我建议使用此设置来测试此变通办法.
  • 当然,您也可以使用访问令牌.但是,在这种情况下,当使用访问令牌时,该示例脚本不能直接用作自定义函数.
  1. 点击查看权限".
  2. 选择自己的帐户.
  3. 在此应用未验证"中单击高级".
  4. 点击转到###项目名称###(不安全)"
  5. 单击允许"按钮.

  • 单击确定".
  • 复制Web应用程序的URL.就像 https://script.google.com/macros/s/###/exec .

    • 修改Google Apps脚本后,请重新部署为新版本.这样,修改后的脚本将反映到Web Apps.请注意这一点.

    ,请将上述脚本的 https://script.google.com/macros/s/###/exec 的URL设置为 url .并请重新部署Web Apps.这样,最新脚本将反映到Web应用程序中.所以请注意这一点.

    Please set the URL of https://script.google.com/macros/s/###/exec to url of above script. And please redeploy Web Apps. By this, the latest script is reflected to the Web Apps. So please be careful this.

    4.测试此替代方法.

    请将 = getYoutubeData("### youtubeId ###")放入单元格.这样,YouTube ID将发送到Web Apps,并且Web Apps返回 vidData.statistics 的值.

    4. Test this workaround.

    Please put =getYoutubeData("###youtubeId###") to a cell. By this, the youtube ID is sent to the Web Apps and the Web Apps returns the values of vidData.statistics.

    • 这些是用于解释变通办法的简单示例脚本.因此,在使用此功能时,请根据实际情况对其进行修改.

    这篇关于在App脚本(js)中运行Youtube数据服务时出错–超出了未经身份验证的使用的每日限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆