Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在应用程序脚本(js)和#x2013中运行Youtube数据服务时出错;超出未经验证使用的每日限制_Javascript_Google Apps Script_Google Cloud Platform_Youtube Api - Fatal编程技术网

Javascript 在应用程序脚本(js)和#x2013中运行Youtube数据服务时出错;超出未经验证使用的每日限制

Javascript 在应用程序脚本(js)和#x2013中运行Youtube数据服务时出错;超出未经验证使用的每日限制,javascript,google-apps-script,google-cloud-platform,youtube-api,Javascript,Google Apps Script,Google Cloud Platform,Youtube Api,我正在应用程序脚本中运行一个自定义函数,它利用Youtube(Youtube数据API v3)高级服务。运行时,出现以下错误: GoogleJsonResponseException:对youtube.videos.list的API调用失败,错误为:超出未经验证使用的每日限制。继续使用需要注册。(第15行) 我不确定如何验证我的应用程序。我已经将它添加到一个云项目中,并启用了API 更新:以下是我的代码: function getYoutubeData(youtubeId) { // Do

我正在应用程序脚本中运行一个自定义函数,它利用Youtube(Youtube数据API v3)高级服务。运行时,出现以下错误:

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

我不确定如何验证我的应用程序。我已经将它添加到一个云项目中,并启用了API

更新:以下是我的代码:

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
}
函数getYoutubeData(youtubeId){ //不要空着肚子跑 如果(!youtubeId){return null} //提出请求 var vidData=YouTube.Videos.list(“统计信息,代码段,{id:youtubeId}”)。项;
如果(!vidData | vidData.length我相信你的目标如下

  • 您希望将脚本中的
    vidData.statistics
    的值放入单元格
  • 您希望使用诸如
    =getYoutubeData(youtubeId)
    之类的自定义函数来实现这一点
对于这个问题,这个答案如何

问题和解决方法: 不幸的是,当自定义函数中使用Advanced Google services的YouTube数据API时,没有使用访问令牌。从您的脚本来看,我认为您出现问题的原因是这样的。例如,当使用
const sample=()=>ScriptApp.getOAuthToken();
函数作为自定义函数时,如
=sample()
,不返回任何值。出于安全考虑,我认为这是Google方面的当前规范

为了在上述情况下实现您的目标,以下变通方法如何

解决方法1: 在这种解决方法中,首先将youtube ID设置为Google电子表格中的单元格。
vidData.statistics
的值由Google Apps脚本检索,该脚本不是自定义函数,并用结果值替换youtube ID

示例脚本:
// 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();
}
请将youtube ID的单元格范围设置为
sourceRange
,并设置工作表名称。在示例中,假设youtube ID放在单元格“A1:A10”中。请在脚本编辑器中运行
getYoutubeData()
。当然,您也可以将其设置为自定义菜单

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);
}
函数getYoutubeData(){ const sourceRange=“A1:A10”;//请设置youtube ID的单元格范围。 const sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”);//请设置工作表名称。 常量范围=sheet.getRange(sourceRange); const youtubeIds=range.getValues(); const values=youtubeIds.map([youtubeId])=>{ //这是你的剧本。 如果(!youtubeId){return[null]} var vidData=YouTube.Videos.list(“统计信息,代码段,{id:youtubeId}”)。项;
如果(!vidData | vidData.length我相信你的目标如下

  • 您希望将脚本中的
    vidData.statistics
    的值放入单元格
  • 您希望使用诸如
    =getYoutubeData(youtubeId)
    之类的自定义函数来实现这一点
对于这个问题,这个答案如何

问题和解决方法: 不幸的是,当自定义函数中使用Advanced Google services的YouTube数据API时,没有使用访问令牌。从您的脚本来看,我认为您出现问题的原因是这样的。例如,当使用
const sample=()=>ScriptApp.getOAuthToken();
函数作为自定义函数时,如
=sample()
,不返回任何值。出于安全考虑,我认为这是Google方面的当前规范

为了在上述情况下实现您的目标,以下变通方法如何

解决方法1: 在这种解决方法中,首先将youtube ID设置为Google电子表格中的单元格。
vidData.statistics
的值由Google Apps脚本检索,该脚本不是自定义函数,并用结果值替换youtube ID

示例脚本:
// 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();
}
请将youtube ID的单元格范围设置为
sourceRange
,并设置工作表名称。在示例中,假设youtube ID放在单元格“A1:A10”中。请在脚本编辑器中运行
getYoutubeData()
。当然,您也可以将其设置为自定义菜单

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);
}
函数getYoutubeData(){ const sourceRange=“A1:A10”;//请设置youtube ID的单元格范围。 const sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”);//请设置工作表名称。 常量范围=sheet.getRange(sourceRange); const youtubeIds=range.getValues(); const values=youtubeIds.map([youtubeId])=>{ //这是你的剧本。 如果(!youtubeId){return[null]} var vidData=YouTube.Videos.list(“统计信息,代码段,{id:youtubeId}”)。项;
如果(!vidData | vidData.length)您能提供您当前的脚本吗?从
我正在应用程序脚本中运行一个自定义函数,该脚本利用Youtube(Youtube数据API v3)高级服务。
,我认为您出现问题的原因可能是由于在自定义函数中请求高级Google服务的API时无法检索访问令牌。但我不确定您的脚本。因此我想我想确认您当前的脚本。添加了我的代码示例@TanaikeA很多人使用术语“自定义函数”不在中,因此您能否澄清您在中使用的是什么上下文?请注意,您不能在自定义函数中调用需要身份验证的服务,但如果在正确的上下文中使用,YouTube API[自动]()感谢您回复并添加您的脚本。从他们那里,我提出了解决方案作为答案。您能确认一下吗?如果这些不是您期望的方向,我很抱歉。您能提供您当前的脚本吗?从
我正在使用Youtube(Youtube数据API v3)的应用程序脚本中运行自定义函数高级服务。
,IT