使用Kraken Python REST API,如何与Sandbox/demo环境交互?

问题描述 投票:0回答:1

我想将 Python 3.9 客户端与 Kraken REST API 结合使用。我正在使用他们在这里发布的代码库 - https://support.kraken.com/hc/en-us/articles/360025180232-REST-API-command-line-client-Python-。我想测试这些调用,因此我最近在 https://demo-futures.kraken.com/ 创建了一个 API 密钥。我遇到的问题是弄清楚演示 API URL 是什么。具体来说,我想使用这段代码

api_domain = "https://api.kraken.com"

...
if api_method in api_private or api_method in api_trading or api_method in api_funding or api_method in api_staking:
    api_path = "/0/private/"
    api_nonce = str(int(time.time()*1000))
    try:
        api_key = open("API_Public_Key").read().strip()
        api_secret = base64.b64decode(open("API_Private_Key").read().strip())
    except:
        print("API public key and API private (secret) key must be in plain text files called API_Public_Key and API_Private_Key")
        sys.exit(1)
    api_postdata = api_data + "&nonce=" + api_nonce
    api_postdata = api_postdata.encode('utf-8')
    api_sha256 = hashlib.sha256(api_nonce.encode('utf-8') + api_postdata).digest()
    api_hmacsha512 = hmac.new(api_secret, api_path.encode('utf-8') + api_method.encode('utf-8') + api_sha256, hashlib.sha512)
    api_request = urllib.request.Request(api_domain + api_path + api_method, api_postdata)
    api_request.add_header("API-Key", api_key)
    api_request.add_header("API-Sign", base64.b64encode(api_hmacsha512.digest()))
    api_request.add_header("User-Agent", "Kraken REST API")
elif api_method in api_public:
    api_path = "/0/public/"
    api_request = urllib.request.Request(api_domain + api_path + api_method + '?' + api_data)
    api_request.add_header("User-Agent", "Kraken REST API")

但我不确定需要将“api_domain”更改为什么才能使用演示期货密钥。当我使用 demo-futures 账户中的 API 密钥与

时,我收到“异常:EAPI:无效密钥”
python-3.x rest demo kraken.com
1个回答
0
投票

要使用 Kraken Python REST API 与沙盒/演示环境交互,您应该将代码中的

api_domain
变量更改为演示环境 URL。演示环境的 URL 是“https://demo-futures.kraken.com”。

api_domain = "https://demo-futures.kraken.com"

参见“基本客户

如果选择沙盒环境,则必须从演示环境的设置页面生成密钥:

https://demo-futures.kraken.com/settings/api

访问Kraken Futures API的标准URL是“https://futures.kraken.com”,可以通过向

https://futures.kraken.com/derivatives/api/v3
发送HTTP请求来访问其端点。但是,对于沙箱环境,您应该使用“https://demo-futures.kraken.com”域。

确保您使用的 API 密钥是从沙箱环境生成的,因为生产环境中的密钥在沙箱环境中不起作用,反之亦然。

© www.soinside.com 2019 - 2024. All rights reserved.