Apps Script API为现有项目返回404错误.错误以HTML而不是JSON的形式返回 [英] Apps Script API returning 404 error for existing project. Error returned as HTML rather than JSON

查看:103
本文介绍了Apps Script API为现有项目返回404错误.错误以HTML而不是JSON的形式返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Apps Script API运行Apps脚本功能.我在控制台中设置了脚本,并为该脚本创建了oauth客户端ID.我配置了授权屏幕,并将脚本部署为API可执行文件.我测试了在同一脚本中调用的api函数,但出现404错误:

I was attempting to run an Apps Script function using the Apps Script API. I set up the script in the console, and created an oauth client ID for the script. I configured the authorisation screen and deployed the script as API executable. I tested the api function calling in the same script but got a 404 error saying:

在此服务器上找不到请求的URL/v1/scripts/{{my_script_id}}:运行.

The Requested URL /v1/scripts/{{my_script_id}}:run was not found on this server.

响应以HTML形式返回.我还注意到,从API调用该脚本时,该脚本似乎使其具有自己的客户端ID.

The response came back as HTML. I also noticed that the script seems to make it's own client ID when it's called from the API.

我尝试禁用并重新启用了无效的API.我认为调用应用程序不在同一个项目中可能是一个问题,但是由于Google文档尚不清楚,因此我不确定该怎么做.

I tried disabling and re-enabling the API which didn't work. I think it may be a problem with the calling application not being in the same project but I'm not sure how to do that as the Google documentation is unclear.

function trigger(){
  var bogus = DriveApp.getRootFolder();
  var argument = ["Value0", "Value1", "Value2", "Value3", "Value4", "Value5"]; 

  //         https://www.googleapis.com/auth/script.external_request
  //         https://www.googleapis.com/auth/spreadsheets

 var postRequest = {
    "Content-Type": "application/json",
    "headers": { "Authorization" : "Bearer " + ScriptApp.getOAuthToken()},
    "function": "setStatus",
    "muteHttpExceptions": true,
    "parameters": [argument],
    "devMode": false
 };

  try{
    var response = UrlFetchApp.fetch("https://script.googleapis.com/v1/scripts/{{my_script_id}}:run", postRequest);
    Logger.log(response);
  }catch(err){
    Logger.log(err);
  }
}

我期望以JSON形式出现某种形式的错误,甚至可能要运行该函数,我得到的是一个HTML文档,该文档在显示时显示404错误.

I expected some form of error in the form of JSON or maybe even for the function to run, what I got was a HTML document which displayed a 404 error when displayed.

推荐答案

您不是 POST 请求.默认的 .fetch 方法是 GET .

You're not POSTing the request. Default .fetch method is GET.

postRequest 对象中添加该对象:

method: "POST",

您的 postRequest 中也缺少

有效载荷.

var postRequest = {
    "method":"POST", //added
    "contentType": "application/json", //key changed
    "headers": { "Authorization" : "Bearer " + ScriptApp.getOAuthToken()},
    "muteHttpExceptions": true,
     "payload": JSON.stringify({   //added
      "function": "setStatus",
      "parameters": argument, //removed []
      "devMode": false
    })
 };

参考文献:

  • UrlfetchApp
  • 脚本:运行
  • 这篇关于Apps Script API为现有项目返回404错误.错误以HTML而不是JSON的形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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