I try to get data from Exchange Gate.io by app script. This is my code:
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)
}
I get the error:
{"label":"INVALID_SIGNATURE","message":"Signature mismatch"}
The Keys is valid problem in code.
to learn more check that: https://www.gate.io/docs/apiv4/en/#retrieve-user-s-total-balances
CodePudding user response:
About the official document, when I directly access the URL, an error occurs. But, when I search it on Google and access it, I could open the document. From the document, I found the documentation for the authorization and the document for "Retrieve user's total balances". And also, I found the following sample python script from the official document.
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())
In your situation, when this script is converted to Google Apps Script, I thought that it is your goal. When this python script is converted to Google Apps Script, it becomes as follows.
Sample script:
Before you use this script, please set the variables of key
and secret
.
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());
}
Note:
When I tested the script using the sample variables of
const key = "sample"
,const secret = "sample"
andconst t = "1234567890.123"
for the python script in the official document and Google Apps Script, the same value ofsignature
could be obtained as follows. Both requests are also the same.603899db07ca29e5240de397b8088271b765925ba29a67267b33ad0b076fc31b0cf98d623878d57bf824b58e5336fd74f1cd101e9377816c34fec2acb9358cb2
So, when you test the above sample Google Apps Script and if an error occurs, please confirm your variables again. And, please show the error message.