
[英]Accessing authenticated Google Cloud Endpoints API from Google Apps Script
[英]Cryptopia API in Google Sheets (Google Apps Script)
在繼續使用Google Apps腳本構建Google SpreadSheet的過程中,我已經完成了Bittrex和Poloniex的平衡,但無法使用Cryptopia。
這是我為Bittrex將JSON對象數組轉換為字符串而奮斗的鏈接
這是一個官方的API鏈接: https : //www.cryptopia.co.nz/Forum/Thread/256
這里有些例子:
這是我的代碼,其中顯示“無效授權標頭”錯誤:
// Get Cryptopia balances
var key = keys.getRange("B4").getValue();
var secret = keys.getRange("C4").getValue();
var baseUrl = 'https://www.cryptopia.co.nz/api/';
var command = "GetBalance";
var url = baseUrl + command;
var signature = key + "POST" + encodeURIComponent(url).toLowerCase() + nonce;
var hmacsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret);
var header_value = "amx " + key + ":" + hmacsignature + ":" + nonce;
var headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' };
var options = {
method: 'POST',
headers: headers
};
var response = UrlFetchApp.fetch("https://www.cryptopia.co.nz/api/GetBalance", options);
var json = JSON.parse(response.getContentText());
}
從示例鏈接中,似乎hmacsignature
是由base64編碼的。 那么下面的修飾呢?
var hmacsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret);
var hmacsignature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret));
nonce
? 如果尚未聲明,則可以使用以下腳本。
var nonce = Math.floor(new Date().getTime() / 1000);
我無法測試。 所以我不知道這是否行得通。 如果這不起作用,對不起。
這個怎么樣? var params = {};
即使沒有請求參數也可能是必需的。 所以我添加了這個和Content-Length
。 然后, secret
是由base64編碼的嗎? 我認為它可能是從其他腳本編碼的。
var key = keys.getRange("B4").getValue();
var secret = keys.getRange("C4").getValue();
var nonce = Math.floor(new Date().getTime() / 1000); // Added
var params = {}; // Added
var baseUrl = 'https://www.cryptopia.co.nz/api/';
var command = "GetBalance";
var url = baseUrl + command;
var requestContentBase64String = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, JSON.stringify(params), Utilities.Charset.UTF_8)); // Added
var signature = key + "POST" + encodeURIComponent(url).toLowerCase() + nonce + requestContentBase64String; // Modified
var hmacsignature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, signature, Utilities.base64Decode(secret), Utilities.Charset.UTF_8)); // Modified
var header_value = "amx " + key + ":" + hmacsignature + ":" + nonce;
var headers = {
"Authorization": header_value,
"Content-Type": 'application/json; charset=utf-8',
"Content-Length" : Utilities.newBlob(JSON.stringify(params)).getBytes().length // Added
};
var options = {
method: 'POST',
headers: headers
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
Utilities.computeHmacSignature(algorithm, value, key)
方法無法正確計算二進制輸入。 value
和key
參數的類型為String
,但Utilities.base64Decode
的結果為Byte[]
。 原始二進制值在從Byte[]
轉換為String
時被更改。
使用jsSHA和cf。 https://stackoverflow.com/a/14007167 。
以下內容可用於計算正確的值,並通過適當的UrlFetchApp.fetch
的options
獲取結果。
.... paste src/sha256.js contents ...
...
var params = {"Currency" : "BTC"};
...
var sha = new jsSHA("SHA-256", "TEXT");
sha.setHMACKey(secret, "B64");
sha.update(signature);
var hmacsignature = sha.getHMAC("B64");
var header_value = "amx " + key + ":" + hmacsignature + ":" + nonce;
var headers = {
"Authorization" : header_value,
};
var options = {
"contentType": 'application/json; charset=utf-8',
"method": 'post',
"headers": headers,
"payload": JSON.stringify(params),
"contentLength": JSON.stringify(params).length
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
Logger.log(json);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.