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

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

问题描述

我正在应用脚本中运行一个自定义函数,该函数利用了 Youtube (YouTube Data 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).

对于这个,这个答案怎么样?

For this, how about this answer?

遗憾的是,在自定义函数中使用高级 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 电子表格中的单元格.并且 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 应用程序用作包装器.这样,授权过程在 Web 应用程序中完成.所以自定义函数可以在没有授权的情况下运行.请执行以下流程.

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.部署网络应用.

  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 应用程序.这样,最新的脚本就会反映到 Web Apps 中.所以请注意这一点.

    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 被发送到网络应用程序,网络应用程序返回 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 Scripts (js) 中运行 Youtube Data Service 时出错 - 超出未经身份验证使用的每日限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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