【问题标题】:API with Google Script Apps带有 Google Script Apps 的 API
【发布时间】:2020-06-29 06:47:31
【问题描述】:

我在使用 Google Apps 与名为 Kissflow 的管理软件交互时遇到问题。

function fun2() {

var id = "yyy";
var apisecretkey = "xxx";
var url ='https://'+id+'.kissflow.com/api/1/verify';
var options = {
  method: 'post',
  headers : {"Authorization" : " Basic " + Utilities.base64Encode(id + ":" + apisecretkey)},
  payload: {
    "grant_type": "client_credentials",
    "scope": "basic+user"
  },
  muteHttpExceptions: true
};
var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
}

我想运行这个 API 文档的简单示例,例如,我的目标是能够通过电子表格中的交互将数据发送到软件。如果你能在这方面帮助我,我将非常感激,我是 API 的新手 :)

出现以下错误:SyntaxError: unexpected token

Kissflow API Documentation

【问题讨论】:

    标签: javascript json api google-apps-script google-sheets


    【解决方案1】:
    JSON.parse(UrlFetchApp.fetch(url, options).getContentText())
    

    从上面的行中删除 JSON.parse() 并使用 Logger.log() 在控制台上输出响应,或者使用 console.log() 将响应输出到浏览器日志并查看结果。如果有错误,它将显示更多用户友好的错误消息。

    【讨论】:

      【解决方案2】:

      虽然我不熟悉 API,但我怀疑问题与以下行有关:

      var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
      

      您正在获取响应(可能是 JSON),然后获取该响应的 文本 内容,然后 尝试像 JSON 一样解析该文本 .

      所以(解决方案 1)如果您将 response 变量设置为等于

      var response = UrlFetchApp.fetch(url, options).getContentText();
      

      并尝试将该变量打印到控制台,您可能会发现您有一些可以使用的东西。

      或者(解决方案 2),您可以尝试:

      var response = JSON.parse(UrlFetchApp.fetch(url, options));
      

      如果您希望使用 JavaScript 对象而不是字符串(假设您调用的 .fetch 方法确实提供了 JSON 格式的响应。)

      【讨论】:

      • 好吧,现在我得到一个不同的错误,muteHttpExceptions: true 显示 Http 页面。我想做curl -H "api_key:<your_api_key>" -X GET https://<your_account_id>.appspot.com/api/1/verify之类的事情,或者使用curl -H "api_key:<your_api_key>" -X POST --data-urlencode "PlaceVisited=Singapore" --data-urlencode "Amount=10000" https://<your_account_id>.appspot.com/api/1/Travel Claim/create 之类的API发布一些东西,返回{ PlaceVisited: "Singapore" Amount: 10000 Id: "Sh2a6b8c51_84e7_11e3_bdd0_ddc2c98b0428" Subject: "Request for Singapore" }之类的东西。
      • 因为我是 API 新手,所以我在尝试将其传递给 javascript 时感到困惑,明白吗?感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多