Javascript 我正在谷歌电子表格中创建一本词典

Javascript 我正在谷歌电子表格中创建一本词典,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我是一个编码新手。 我试图在google应用程序脚本中创建一个函数,它的作用类似于一个字典,并提取作为参数传递的单词的含义。它使用牛津词典的API,但不起作用。它显示错误403。“var response=UrlFetchApp.fetch(url,标题);”显示错误 function Word_meaning(word){ var url="https://odapi.oxforddictionaries.com:443/api/v1/entries/en/" + word + "/

我是一个编码新手。 我试图在google应用程序脚本中创建一个函数,它的作用类似于一个字典,并提取作为参数传递的单词的含义。它使用牛津词典的API,但不起作用。它显示错误403。“var response=UrlFetchApp.fetch(url,标题);”显示错误

function Word_meaning(word){ 
    var url="https://odapi.oxforddictionaries.com:443/api/v1/entries/en/" + word + "/regions=us";
    var headers =
    {
        'Accept': 'application/json',
        'app_id': 'abc',
        'app_key': '123'
    };
    var response = UrlFetchApp.fetch(url,headers);
    var data = JSON.parse(response.getContentText());
    Logger.log(data);
}

有两件事-为什么在API调用中包含端口号?我查询牛津字典的API端点看起来不同。此外,“od api”中还有一个破折号

在地址栏中测试链接时,我得到了预期的服务器响应“Authorization required”,而您提供的URL似乎不存在

无论如何,会弹出错误,因为UrlFetchApp.fetch(url,params)方法的可选“params”对象构造不正确。“headers”属性必须包含在该对象中。此处有点含糊不清,但请阅读:

我能够使用下面的代码启动和运行

function getData(word, region){

  var word = word || "leprechaun";
  var region = region || "us";
  var wordId = encodeURI(word);
  var baseUrl = "https://od-api.oxforddictionaries.com/api/v1/entries/en/{word_id}/regions={region}";
  var app_id = "app_id";
  var app_key = "app_key";

  var headers = { 
                  "app_id": app_id, 
                  "app_key": app_key
                 };

  var options = { 
        "headers": headers, 
         "muteHttpExceptions": true 
          };

  var url = baseUrl.replace("{word_id}", wordId)
                   .replace("{region}", region);


  var res = UrlFetchApp.fetch(url, options);
  var responseCode = res.getResponseCode();


  if (responseCode == 200) {

    var data = JSON.parse(res.getContentText());

  } else {

    Logger.log(res.getContentText());


  }


}

请提供更多关于您面临的问题和预期结果的详细信息。请阅读我已详细阐述的细节。好心的建议。给你什么建议?如何使用牛津API?你看过他们的API文档了吗?它将告诉您如何连接和验证。还可以考虑阅读关于 URLFETCHAPP< <代码>的文档,以确保您正确地指定可选参数。您可以在发送请求之前构建请求,从而验证它是否按照您的预期构建。这个简单的调试是您提问前的要求……对此表示歉意,并感谢您的帮助。@Param好的,很高兴我能提供帮助。你能把问题标记为已解决吗?好的,当然可以。谢谢
function getData(word, region){

  var word = word || "leprechaun";
  var region = region || "us";
  var wordId = encodeURI(word);
  var baseUrl = "https://od-api.oxforddictionaries.com/api/v1/entries/en/{word_id}/regions={region}";
  var app_id = "app_id";
  var app_key = "app_key";

  var headers = { 
                  "app_id": app_id, 
                  "app_key": app_key
                 };

  var options = { 
        "headers": headers, 
         "muteHttpExceptions": true 
          };

  var url = baseUrl.replace("{word_id}", wordId)
                   .replace("{region}", region);


  var res = UrlFetchApp.fetch(url, options);
  var responseCode = res.getResponseCode();


  if (responseCode == 200) {

    var data = JSON.parse(res.getContentText());

  } else {

    Logger.log(res.getContentText());


  }


}