这是我第一次尝试使用Google Script的JavaScript,而且我已经碰壁了。
我在使用Google Scripts从API获取数据方面提供了一些帮助。 API需要对某些功能进行身份验证,而且我无法使其正常运行。
以下是从加密交易网站上的帐户获取余额的代码:
function Hash() {
var SystemTimeMilli = Date.now() //This line will get the current time in milliseconds
var accountBalInputString = '/account/balance' + '\n' + SystemTimeMilli +'\n'; //This line builds the string needed to be encrypted
var PrivateKey = 'bEDtDJnW0y/Ll4YZitxb+D5sTNnEpQKH67EJRCmQCqN9cvGiB8+IHzB7HjsOs3mSlxLmu4aiPDRpe9anuWzylw==' //Sample private key (I know I need to use my own)
var BTCsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, accountBalInputString, PrivateKey, Utilities.Charset.UTF_8); //This line encrypts the above string using the above key
BTCsignature = Utilities.base64Encode(BTCsignature); //This converts the output of the above encryption to a base64 format
var BTCbalanceParams = {
'Accept' : 'application/json',
'Accept-Charset' : 'UTF-8',
'Content-Type' : 'application/json',
'apikey' : 'bbr34267-dfgh-4pop-90ty-cdc9f4156a37', //another made up example
'timestamp' : SystemTimeMilli,
'signature' : BTCsignature
};
var BTCbalance =
UrlFetchApp.fetch('https://api.btcmarkets.net/account/balance', BTCbalanceParams);
网站提供的使用其API身份验证的说明可在此处找到:https://github.com/BTCMarkets/API/wiki/Authentication。
我一直在努力
Returned Data from BTC: {"success":false,"errorCode":1,"errorMessage":"Authentication failed."}
我已经尝试了我能想到的一切。
我假设Date.now()以毫秒为单位获得UTC时间的时间,我也假设API希望时间采用相同的格式(尽管我已经尝试将其操作为对我的本地更正时间)。
否则有什么明显的我做错了或值得尝试?
谢谢!
答案 0 :(得分:0)
当它从您显示的URL中读取如何使用API时,必须将BTCbalanceParams
定义为标题。方法是GET。当这反映在您的脚本中时,修改后的脚本如下所示。
function Hash() {
var SystemTimeMilli = Date.now() //This line will get the current time in milliseconds
var accountBalInputString = '/account/balance' + '\n' + SystemTimeMilli +'\n'; //This line builds the string needed to be encrypted
var PrivateKey = 'bEDtDJnW0y/Ll4YZitxb+D5sTNnEpQKH67EJRCmQCqN9cvGiB8+IHzB7HjsOs3mSlxLmu4aiPDRpe9anuWzylw==' //Sample private key (I know I need to use my own)
var BTCsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, accountBalInputString, PrivateKey, Utilities.Charset.UTF_8); //This line encrypts the above string using the above key
BTCsignature = Utilities.base64Encode(BTCsignature); //This converts the output of the above encryption to a base64 format
var BTCbalanceParams = {
'Accept' : 'application/json',
'Accept-Charset' : 'UTF-8',
'Content-Type' : 'application/json',
'apikey' : 'bbr34267-dfgh-4pop-90ty-cdc9f4156a37', //another made up example
'timestamp' : SystemTimeMilli,
'signature' : BTCsignature
};
// Added
var params = {
method: "get",
headers: BTCbalanceParams,
muteHttpExceptions: true
};
var BTCbalance = UrlFetchApp.fetch('https://api.btcmarkets.net/account/balance', params); // Modified
}
Date.now()
以毫秒为单位检索时间。看来这个API也是必需的。如果这对你没用,我很抱歉。