我迷上了使用HMAC SHA256进行api认证。这是我第一次使用它,我不知道我遗漏了什么,尽管我怀疑它与时间戳有关。有人能帮我确认一下我错过了什么吗?
每次我尝试进行API调用时,都会得到一个声明data: { success: false, error: 'Not logged in: Invalid signature' }的响应。
下面是进行API调用的要求,包括HMAC SHA256。

下面是我目前使用的代码:
const axios = require('axios');
var forge = require('node-forge');
require('dotenv').config()
// get timestamp
var time = new Date().getTime();
// generate and return hash
function generateHash(plainText,secretKey)
{
var hmac = forge.hmac.create();
hmac.start('sha256', secretKey);
hmac.update(plainText);
var hashText = hmac.digest().toHex();
return hashText
}
// set axios config
var config = {
url:"https://ftx.us/api/wallet/all_balances",
method:"GET",
headers :{
"FTXUS-KEY":process.env.FTX_API_KEY,
"FTXUS-TS":time,
"FTXUS-SIGN":generateHash(`${new Date()}${"GET"}${"/wallet/all_balances"}`,process.env.FTX_API_SECRET)
}
}
axios(config)
.then(response => {
console.log(response.data)
}).catch(function (error) {
console.log(error);
})
发布于 2022-07-02 14:47:01
我不得不经历同样的问题,所以这是我的代码。
import * as crypto from "crypto";
import fetch from "node-fetch";
// a function to call FTX (US)
async function callFtxAPIAsync(secrets, method, requestPath, body) {
const timestamp = Date.now();
const signaturePayload = timestamp + method.toUpperCase() + "/api" + requestPath + (method.toUpperCase() == "POST" ? JSON.stringify(body) : "");
const signature = crypto.createHmac('sha256', secrets.secret)
.update(signaturePayload)
.digest('hex');
const response = await fetch("https://ftx.us/api" + requestPath, {
method: method,
body: body != null ? JSON.stringify(body) : "",
headers: {
'FTXUS-KEY': secrets.key,
'FTXUS-TS': timestamp.toString(),
'FTXUS-SIGN': signature,
"Content-Type": "application/json",
"Accepts": "application/json"
}
});
return await response.json();
}然后调用一个post端点,例如:
let resultQuote = await callFtxAPIAsync(secrets, "post", "/otc/quotes",
{
"fromCoin": "USD",
"toCoin": "ETH",
"size": usd
});或者得到一个:
let resultQuote = await callFtxAPIAsync(secrets, "get", "/otc/quotes/1234");我希望它能帮上忙
发布于 2022-06-17 17:25:00
您需要添加完整的URL路径,但域除外,在您的示例中,缺少/api。试试这个:
"FTXUS-SIGN":generateHash(`${new Date()}${"GET"}${"/api/wallet/all_balances"}`,process.env.FTX_API_SECRET)https://stackoverflow.com/questions/72129385
复制相似问题