Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created May 24, 2022 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/d0ea117b1c0e54cf713a8027f6b2fb08 to your computer and use it in GitHub Desktop.
Save tanaikech/d0ea117b1c0e54cf713a8027f6b2fb08 to your computer and use it in GitHub Desktop.
Requesting to Gate API v4 using Google Apps Script

Requesting to Gate API v4 using Google Apps Script

This is a sample script for requesting to Gate API v4 using Google Apps Script.

The official document of Gate API v4 is here. Recently, I answered this thread. In that case, in order to convert the sample python script to Google Apps Script, the script for retrieving the signature might be a bit complicated. So, here, I would like to introduce this.

Sample python script from official document

This is a sample python script from 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 this post, this script is converted to Google Apps Script.

First, as the sample values, key, secret and t are sample, sample and 1234567890.123, respectively. When these values are used to the above script, the value of sign is 603899db07ca29e5240de397b8088271b765925ba29a67267b33ad0b076fc31b0cf98d623878d57bf824b58e5336fd74f1cd101e9377816c34fec2acb9358cb2.

Converted Google Apps Script from python script

When the above python script is converted to Google Apps Script, it becomes as follows.

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());
}

Here, as the sample values, key, secret and t are sample, sample and 1234567890.123, respectively. The value of sign is 603899db07ca29e5240de397b8088271b765925ba29a67267b33ad0b076fc31b0cf98d623878d57bf824b58e5336fd74f1cd101e9377816c34fec2acb9358cb2. By this, it can be confirmed that both values of sign are the same between the python script and the Google Apps Script.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment