将查询字符串转换为请求正文响应错误

时间:2019-02-02 17:50:23

标签: google-apps-script

我正在尝试将有效的查询字符串转换为请求正文,并且收到错误消息,提示我缺少属性。我从@Tanaike的另一篇文章中获得了帮助,该文章提供了编码URI函数,该函数将参数分解为编码格式。但是,当我的“ pairs”参数中有30-50对时,URLFetch的查询字符串长度限制有错误。因此,我尝试将其作为请求正文发送。

var botParams = {
"name": "TestBot",
"base_order_volume": 0.001,
"take_profit": 1.5,
"safety_order_volume": 0.001,
"martingale_volume_coefficient": 2,
"martingale_step_coefficient": 1,
"max_safety_orders": 1,
"active_safety_orders_count": 1,
"safety_order_step_percentage": 2.5,
"take_profit_type": "total",
"stop_loss_percentage": 0,
"cooldown": 0,
"pairs": [
  "BTC_ADA",
  "BTC_TRX"
],
"trailing_enabled":"true",
"trailing_deviation":0.5,
"strategy_list": [
  {
    "strategy":"cqs_telegram"
  }
]
  };    

  try {

//    var totalParams = keys.reduce(function(q, e, i) {
//      q += (e == "pairs" ? botParams[e].reduce(function(s, f, j) {
//        s += e + "[]=" + f + (j != botParams[e].length - 1 ? "&" : "");
//        return s;
//      },"") : e + "=" + (typeof botParams[e] == "object" ? 
//encodeURIComponent(JSON.stringify(botParams[e])) : 
//encodeURIComponent(botParams[e]))) + (i != keys.length - 1 ? "&" : "");
//      return q;
//    }, endPoint); //Thanks to Tanaike


//Call
//Base
var baseUrl = "https://3commas.io";        
//Total Endpoint
var endPoint = "/public/api/ver1/bots/274339/update?";


//Convert Bot Params + endPoint to Encoded URI
var keys = Object.keys(botParams);
var totalParams = keys.reduce(function(q, e, i) {
  q += e + "=" + (typeof botParams[e] == "object" ? encodeURIComponent(JSON.stringify(botParams[e])) : encodeURIComponent(botParams[e])) + (i != keys.length - 1 ? "&" : "");
  Logger.log(encodeURIComponent(botParams[e]))
  return q;
},endPoint);

Logger.log(totalParams)   

//Create Signature
var signature = Utilities.computeHmacSha256Signature(totalParams, secret);    
//Convert from byte
signature = signature.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");

var headers = {
  "APIKEY": key,
  "Signature": signature,
};
var params = {
  "method": "PATCH",
  "headers": headers,     
  "payload" : totalParams,
  //Show full exceptions      
   muteHttpExceptions: true
};  

var finalURL = baseUrl + "/public/api/ver1/bots/274339/update";

Logger.log(finalURL)
//https://3commas.io/public/api/ver1/bots/274339/update

// Call final URL with params
var data = UrlFetchApp.fetch(finalURL, params).getContentText();
var json = JSON.parse(data);
Logger.log(json)
  } catch (err) {Logger.log(err)}
}

我当前收到的错误是{error_attributes={name=[is missing]}, error_description=Invalid parameters, error=record_invalid},因此由于某种原因,它无法识别我的姓名属性。当我记录结果时,我看到名称是作为字符串传递的。

记录的结果:/public/api/ver1/bots/274339/update?name=TestBot&base_order_volume=0.001&take_profit=1.5&safety_order_volume=0.001&martingale_volume_coefficient=2&martingale_step_coefficient=1&max_safety_orders=1&active_safety_orders_count=1&safety_order_step_percentage=2.5&take_profit_type=total&stop_loss_percentage=0&cooldown=0&pairs=%5B%22BTC_ADA%22%2C%22BTC_TRX%22%5D&trailing_enabled=true&trailing_deviation=0.5&strategy_list=%5B%7B%22strategy%22%3A%22cqs_telegram%22%7D%5D

3个逗号文档:https://github.com/3commas-io/3commas-official-api-docs/blob/master/bots_api.md#edit-bot-permission-bots_write-security-signed

只需寻找有关可能的建议。谢谢。

编辑:对于将来的读者-我最后回到了关于主题PATCH query string 3commas的第一篇文章中使用URL缩短API服务的查询字符串方法。这项工作可以解决我的URLFetch长度限制错误,这是尝试请求主体方法的主要原因。 URL shortner我曾经用过。非常简单,只需使用常规的提取操作即可获取链接。

2 个答案:

答案 0 :(得分:1)

我认为,在您的情况下,可能需要let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusItem.button?.title = "My Title" 作为表单发送,而需要从botParams创建查询参数以创建签名。因此,修改后的脚本如下。

修改后的脚本:

botParams

注意:

  • 我不确定API的规格。因此,如果上述脚本不起作用,您可以向API管理器询问以下几点吗?
    1. 关于var key = 'apikey'; var secret = 'apisecret'; var editBots = "/ver1/bots/274339/update"; var baseUrl = "https://3commas.io"; var endPoint = "/public/api"+editBots; var botParams = { "name": "TestBot", "base_order_volume": 0.001, "take_profit": 1.5, "safety_order_volume": 0.001, "martingale_volume_coefficient": 2, "martingale_step_coefficient": 1, "max_safety_orders": 1, "active_safety_orders_count": 1, "safety_order_step_percentage": 2.5, "take_profit_type": "total", "stop_loss_percentage": 0, "cooldown": 0, "pairs": ["BTC_ADA", "BTC_TRX"], "trailing_enabled": "true", "trailing_deviation": 0.5, "strategy_list": [{"strategy": "cqs_telegram"}] }; var keys = Object.keys(botParams); var totalParams = keys.reduce(function(q, e, i) {return q += (e == "pairs" ? botParams[e].reduce(function(s, f, j) {return s += e + "=" + f + (j != botParams[e].length - 1 ? "&" : "")},"") : e + "=" + (typeof botParams[e] == "object" ? encodeURIComponent(JSON.stringify(botParams[e])) : encodeURIComponent(botParams[e]))) + (i != keys.length - 1 ? "&" : "")}, endPoint + "?"); var signature = Utilities.computeHmacSha256Signature(totalParams, secret); signature = signature.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join(""); var headers = { 'APIKEY': key, 'Signature': signature, }; var params = { method: 'PATCH', headers: headers, payload: Object.keys(botParams).reduce(function(o, e) { o[e] = typeof botParams[e] == "object" ? JSON.stringify(botParams[e]) : e; return o; }, {}), muteHttpExceptions: true }; var data = UrlFetchApp.fetch(baseUrl + endPoint, params).getContentText(); Logger.log(data) 中的"pairs": ["BTC_ADA", "BTC_TRX"],,在此脚本中,使用与创建查询参数相同的过程,例如botParams。这是正确的吗?
    2. 要创建pairs=BTC_ADA&pairs=BTC_TRXkeysecret是否需要包含在botParams中?
    3. 是否需要将signaturekey包括在有效载荷中?
    4. 是否同时需要2和3?

编辑:

修改点:

从:
secret
至:
  payload: Object.keys(botParams).reduce(function(o, e) {
    o[e] = typeof botParams[e] == "object" ? JSON.stringify(botParams[e]) : e;
    return o;
  }, {}),

答案 1 :(得分:1)

我发现了问题。怀疑这是一个编码问题。这就是最终的结果。

        var totalParams2 = keys.reduce(function(q, e, i) {
          q += (e == "pairs" ? botParams[e].reduce(function(s, f, j) {
            s += e + encodeURIComponent("[]")+"=" + f + (j != botParams[e].length - 1 ? "&" : "");            
            return s;
          },"") : e + "=" + (typeof botParams[e] == "object" ? encodeURIComponent(JSON.stringify(botParams[e])) : encodeURIComponent(botParams[e]))) + (i != keys.length - 1 ? "&" : "");
          return q;
        }, "");

添加e + encodeURIComponent("[]")+的原因特别在于API的设置方式。我必须对对数组进行编码。