我在使用 FTX api 时遇到问题

分享于2022年07月17日 api python python-requests 问答
【问题标题】:我在使用 FTX api 时遇到问题(I have trouble with FTX api)
【发布时间】:2022-07-15 20:56:13
【问题描述】:

我想使用 FTX api 获取余额。

参考 api docs 中的Python示例代码,修改如下。

但它返回一条错误消息。

{"success":false,"error":"未登录:无效签名"}

不知道为什么签名不对。

有人可以帮忙吗?

import json
import hmac
import time
import requests

# API Keys
with open('../json/api.json', 'r') as f:
    api = json.load(f)
    accessKey = api['FTX']['ACCESS']
    secretKey = api['FTX']['SECRET']

endpoint = 'https://ftx.com/api'
url = '/wallet/balances'
method = 'GET'

ts = int(time.time() * 1000)
signature_payload = f'{ts}{method}{url}'.encode()
signature = hmac.new(secretKey.encode(), signature_payload, 'sha256').hexdigest()

headers = {
    'FTX-KEY': accessKey,
    'FTX-SIGN': signature,
    'FTX-TS': str(ts)
}

res = requests.request(method, endpoint+url, headers=headers)
print(res.text)

参考

FTX API Authentication
FTX API Get Balance


【解决方案1】:

尝试将 url 更改为 ftx.us 和标题

'FTXUS-KEY': accessKey,
'FTXUS-SIGN': signature,
'FTXUS-TS': str(ts)