i think the issue is with the signature, the request body is same as in the independentreserve api docs. i am using apps script to connect with api, i tried using python and it works fine, but i am new to javascript and google apps script. this is my code. can someone help with this?
function myFunction() {
var key = 'api-key'
var secret = 'api-secret'
var url = 'https://api.independentreserve.com/Private/GetOpenOrders'
// initialize nonce to current unix time in milliseconds
nonce = (new Date()).getTime();
// Set custom User-Agent string
var headers = {"User-Agent": "Independent Reserve Javascript API Client"};
var nonce = nonce ;
console.info("hELLO")
var message = [url, 'apiKey=' key, 'nonce=' nonce].join(',') ;
//var signer = crypto.createHmac('sha256', Buffer(secret, 'utf8'));
var signer = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, secret);
var signature = signer
.map(function(byte) {
// Convert from 2's compliment
var v = (byte < 0) ? 256 byte : byte;
// Convert byte to hexadecimal
return ("0" v.toString(16)).slice(-2);
}).join(',');
var headers = {'Content-Type': 'application/json', 'muteHttpExceptions': true};
var options = {
"apiKey": key,
"nonce": nonce,
"signature": signature,
"primaryCurrencyCode": "Xbt",
"secondaryCurrencyCode": "Usd",
"pageIndex": 1,
"pageSize": 25
}
var r = UrlFetchApp.fetch(url, options)
console.info(r.getContentText())
}
CodePudding user response:
I believe your goal is as follows.
- In order to request to
api.independentreserve.com
, you want to convert the following sample script of python. Ref
When I saw your script, I thought the following modification points:
Utilities.computeDigest
will beUtilities.computeHmacSha256Signature
.(new Date()).getTime()
cannot be directly used. In this case, I thought that it's(new Date()).getTime().toString().slice(0, 10)
.muteHttpExceptions
cannot be used in the request header.'Content-Type': 'application/json'
can be used ascontentType: "application/json"
. And in this case, it is required to convert the JSON object to the string value.
When the above python script is converted to Google Apps Script by modifying these points, it becomes as follows.
Sample script:
function myFunction() {
var key = 'api_key';
var secret = 'api_secret';
var url = 'https://api.independentreserve.com/Private/GetOpenOrders';
var nonce = (new Date()).getTime().toString().slice(0, 10);
var parameters = [
url,
'apiKey=' key,
'nonce=' nonce.toString(),
'primaryCurrencyCode=Xbt',
'secondaryCurrencyCode=Usd',
'pageIndex=1',
'pageSize=10'
];
var message = parameters.join(",");
var signature = Utilities.computeHmacSha256Signature(message, secret, Utilities.Charset.UTF_8).map(b => ('0' (b & "0xFF").toString(16)).slice(-2)).join('');
var payload = {
"apiKey": key,
"nonce": nonce,
"signature": signature,
"primaryCurrencyCode": "Xbt",
"secondaryCurrencyCode": "Usd",
"pageIndex": 1,
"pageSize": 25
};
var options = {
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true,
};
var r = UrlFetchApp.fetch(url, options);
console.log(r.getContentText());
}
Note:
- I confirmed that this request of the above Google Apps Script is the same as the sample python script. So, when an error occurs, please confirm your
key
,secret
, and the values ofparameters
, again.