Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 14, 2023 19:32
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tanaikech/175067567819577fd8eba9b82eabd1a6 to your computer and use it in GitHub Desktop.
Save tanaikech/175067567819577fd8eba9b82eabd1a6 to your computer and use it in GitHub Desktop.
Binance API for Google Apps Script

Binance API for Google Apps Script

This sample script is for using Binance API by Google Apps Script. This script encryptes "signature" like samples. In this script, "Example 1: As a query string" is used, and it retrieves "All orders (SIGNED)" by "GET".

function main() {
    var key = '#####'; // Please input your key.
    var secret = '#####'; // Please input your secret.
    var api = "/api/v3/allOrders"; // Please input API Endpoint you want.
    var timestamp = Number(new Date().getTime()).toFixed(0);
    var string = "symbol=LTCBTC&timestamp=" + timestamp; // Please input query parameters for the inputterd API.

    var baseUrl = "https://api.binance.com";
    var signature = Utilities.computeHmacSha256Signature(string, secret);
    signature = signature.map(function(e) {
        var v = (e < 0 ? e + 256 : e).toString(16);
        return v.length == 1 ? "0" + v : v;
    }).join("");
    var query = "?" + string + "&signature=" + signature;
    var params = {
        'method': 'get',
        'headers': {'X-MBX-APIKEY': key},
        'muteHttpExceptions': true
    };
    var data = UrlFetchApp.fetch(baseUrl + api + query, params);
    Logger.log(data.getContentText())
}
@NDNewell
Copy link

Thank you!

@tanaikech
Copy link
Author

Welcome. Thank you, too.

@Anastasgrek
Copy link

[20-08-09 00:31:36:197 MSK] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: vAd8yZwTaG4dCuBGJJLbb_zPU_NwKrSMkgaI5B7Ow2iRt3_ro1Luyw==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

I got this error! What can I do?

@asterix1967-github
Copy link

Dear,
could you help me ? , Please !

Few days , I try to run with this Binance API

do you have a idea? it's about Query String and Body query , but really I don't find a solution

It will be great if I can receive a help ! Thanks !

https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#general-information-on-endpoints

function binance_test_servertime() {

    
    var baseUrl = "https://api.binance.com";
    var api = "/api/v3/time"; 
    
    var key = 'xxxxxxxxxxxxxxxxx';
    var secret = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
    
    var timestamp = Number(new Date().getTime()).toFixed(0);
    
    var string = "timestamp=" + timestamp; 

    var signature = Utilities.computeHmacSha256Signature(string, secret);
    signature = signature.map(function(e) {
        var v = (e < 0 ? e + 256 : e).toString(16);
        return v.length == 1 ? "0" + v : v;
    }).join("");

    var query = "?" + string + "&signature=" + signature;
    var params = {
        'method': 'get',
        'headers': {'X-MBX-APIKEY': key},
        'muteHttpExceptions': true
    };
    
    Logger.log(baseUrl+api+query);
    
    var data = UrlFetchApp.fetch(baseUrl + api + query, params);

    Logger.log(data.getContentText())
    
}

I will have always this return from API


[20-11-19 11:58:39:597 CET] https://api.binance.com/api/v3/time?timestamp=1605783519594&signature=b7a3b1fb601161882c386980d706a2ab3f912f97f6e089a0d11141c124393138
[20-11-19 11:58:39:626 CET] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: pKVVim3JseuNqDFW_5M7BBjiA9BykmeTMlryp6-mFwCsoGd6ODXQHQ==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

@htadashi
Copy link

@Anastasgrek and @asterix1967-github
I believe this happens because GoogleAppsScript automatically sets up the X-Forwarded-For header when these functions are ran. This may be activating a Cloudflare protection which blocks the request.

You could try to use @tanaikech sensei workaround here, but please note that a wrong setup of the webapp could let others run these functions without your permission.

@daksh890
Copy link

@htadashi Could you please elaborate the steps. I am also facing the same issue. Thanks

@kopax-polyconseil
Copy link

Just use a proxy instead of spamming from google sheets ips address serveur, thanks @tanaikech for this !

@Pavel-Monolit
Copy link

Дай Бог тебе Здоровья, хороший человек!!! 3 часа искал тебя

@axisrow
Copy link

axisrow commented Jan 14, 2023

Информация {
"code": 0,
"msg": "Service unavailable from a restricted location according to 'b. Eligibility' in https://www.binance.com/en/terms. Please contact customer service if you believe you received this message in error."
}

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