gpt4 book ai didi

javascript - 使用 Google 脚本对 API 调用进行身份验证

转载 作者:行者123 更新时间:2023-11-28 03:53:04 25 4
gpt4 key购买 nike

这是我第一次尝试使用 Google 脚本编写 JavaScript,但遇到了困难。

我需要一些有关使用 Google 脚本从 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 .

我不断地得到BTC 返回数据:{"success":false,"errorCode":1,"errorMessage":"身份验证失败。"}

我已经尝试了我能想到的一切。

我假设 Date.now() 获取以毫秒为单位的 UTC 时间时间,并且我还假设 API 希望时间采用相同的格式(尽管我已尝试对其进行操作以使其与我的本地时间保持一致) .

否则有什么明显的我做错了或者值得尝试的事情吗?

谢谢!

最佳答案

当它从您显示的 URL 读取如何使用 API 时,必须将 BTCbalanceParams 定义为 header 。方法是GET。当这反射(reflect)到您的脚本中时,修改后的脚本如下。

修改后的脚本:

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也是需要这个的。

如果这对您没有用,我很抱歉。

关于javascript - 使用 Google 脚本对 API 调用进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47806570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com