应用程序脚本中的门 io 错误 INVALID_SIGNATURE

Gate io error INVALID_SIGNATURE in app script

我尝试通过应用程序脚本从 Exchange Gate.io 获取数据。 这是我的代码:

function data() {
var key = "***"
var sec = "***"
var timestamp = Math.floor(Date.now() / 1000)

var base = "https://api.gateio.ws";
var prefix = "/api/v4"
var pat = '/wallet/total_balance'

var sign = "timestamp=" + timestamp;

Logger.log(sign)


  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, sign, sec);
  signature = signature.map(function(e) {
    var v = (e < 0 ? e + 256 : e).toString(16);
    return v.length == 1 ? "0" + v : v;
}).join("");
 

    var params = {
        'method': "get",
        'headers': {
                      'Accept': 'application/json', 
                      'Content-Type': 'application/json',
                       'KEY': key, 
                       'Timestamp': timestamp,
                       'SIGN': signature,
                       
                      },
        'muteHttpExceptions': true,
    };

   var data = UrlFetchApp.fetch(base + prefix + pat, headers=params);
   Logger.log(data)

}

我收到错误:

{"label":"INVALID_SIGNATURE","message":"Signature mismatch"}

密钥是代码中的有效问题。

要了解更多信息,请检查:https://www.gate.io/docs/apiv4/en/#retrieve-user-s-total-balances

关于官方文档,直接访问URL时出现错误。但是,当我在 Google 上搜索并访问它时,我可以打开该文档。从文档中找到了授权文档和“获取用户总余额”的文档。而且,我从官方文档中找到了以下示例 python 脚本。

import time
import hashlib
import hmac
import requests

def gen_sign(method, url, query_string=None, payload_string=None):
    key = ''        # api_key
    secret = ''     # api_secret

    t = time.time()
    m = hashlib.sha512()
    m.update((payload_string or "").encode('utf-8'))
    hashed_payload = m.hexdigest()
    s = '%s\n%s\n%s\n%s\n%s' % (method, url, query_string or "", hashed_payload, t)
    sign = hmac.new(secret.encode('utf-8'), s.encode('utf-8'), hashlib.sha512).hexdigest()
    return {'KEY': key, 'Timestamp': str(t), 'SIGN': sign}

host = "https://api.gateio.ws"
prefix = "/api/v4"
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}

url = '/wallet/total_balance'
query_param = ''
# for `gen_sign` implementation, refer to section `Authentication` above
sign_headers = gen_sign('GET', prefix + url, query_param)
headers.update(sign_headers)
r = requests.request('GET', host + prefix + url, headers=headers)
print(r.json())

在您的情况下,当此脚本转换为 Google Apps 脚本时,我认为这是您的目标。当这个python脚本转换为GoogleApps脚本后,就变成了下面这样。

示例脚本:

在使用此脚本前,请设置keysecret的变量。

function gen_sign_(key, secret, method, url, query_param = "", payload_string = "") {
  const t = (Date.now() / 1000).toString();
  const c1 = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_512, payload_string, Utilities.Charset.UTF_8);
  const c2 = Utilities.formatString('%s\n%s\n%s\n%s\n%s', method, url, query_param, c1.map(b => ("0" + (b < 0 && b + 256 || b).toString(16)).slice(-2)).join(""), t);
  const c3 = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, c2, secret, Utilities.Charset.UTF_8);
  const sign = c3.map(b => ("0" + (b < 0 && b + 256 || b).toString(16)).slice(-2)).join("");
  return { "KEY": key, "Timestamp": t, "SIGN": sign };
}

// Please run this function.
function main() {
  const key = "your api key";
  const secret = "your secret";

  const host = "https://api.gateio.ws";
  const prefix = "/api/v4";
  const url = "/wallet/total_balance";
  const method = "GET";

  const signature = gen_sign_(key, secret, method, prefix + url);
  const headers = { "Accept": "application/json", "Content-Type": "application/json", "muteHttpExceptions": true, ...signature };
  const res = UrlFetchApp.fetch(host + prefix + url, { method, headers });
  console.log(res.getContentText());
}

注:

  • 当我使用const key = "sample"const secret = "sample"const t = "1234567890.123"的示例变量测试脚本时,对于官方文档中的python脚本和Google Apps Script, signature 的相同值可以通过以下方式获得。两个请求也是一样的。

      603899db07ca29e5240de397b8088271b765925ba29a67267b33ad0b076fc31b0cf98d623878d57bf824b58e5336fd74f1cd101e9377816c34fec2acb9358cb2
    
  • 因此,当您测试上述示例Google Apps Script 时,如果出现错误,请再次确认您的变量。并且,请显示错误消息。

参考文献: