首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bitfinex api v2错误,密钥无效

bitfinex api v2错误,密钥无效
EN

Stack Overflow用户
提问于 2017-06-30 00:55:56
回答 2查看 3.5K关注 0票数 4

我正在尝试对他们新的v2应用程序接口进行一个基本的身份验证应用程序接口调用,结果返回了一个无效的应用程序接口密钥错误。

我重新发出api密钥只是为了验证,同样的错误。

代码语言:javascript
复制
from time import time
import urllib.request
import urllib.parse
import hashlib
import hmac

APIkey = b'myapikeyyouarenotsupposedtosee'
secret = b'myceeeeecretkeyyyy'

url = 'https://api.bitfinex.com/v2/auth/r/wallets'

payload = {
    #'request':'/auth/r/wallets',
    'nonce': int(time() * 1000),
}

paybytes = urllib.parse.urlencode(payload).encode('utf8')
print(paybytes)

sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()
print(sign)

headers = {
    'Key': APIkey,
    'Sign': sign
}

req = urllib.request.Request(url, headers=headers, data=paybytes)
with urllib.request.urlopen(req) as response:
    the_page = response.read()
    print(the_page)

如何通过身份验证调用bitfinex新的v2接口?

EN

回答 2

Stack Overflow用户

发布于 2017-07-29 15:34:22

您的标题是错误的。我也在尝试这样做,并尝试使用bitfinex文档中的example code,但是他们的示例包含一个错误,因为他们需要首先将字符串编码为v2 -8字节数组。因此,我已经修复了它,并发布了下面的整个示例。

代码语言:javascript
复制
#
# Example Bitfinex API v2 Auth Python Code
#
import requests  # pip install requests
import json
import base64
import hashlib
import hmac
import os
import time #for nonce

class BitfinexClient(object):
    BASE_URL = "https://api.bitfinex.com/"
    KEY = "API_KEY_HERE"
    SECRET = "API_SECRET_HERE"

    def _nonce(self):
        # Returns a nonce
        # Used in authentication
        return str(int(round(time.time() * 10000)))

    def _headers(self, path, nonce, body):
        secbytes = self.SECRET.encode(encoding='UTF-8')
        signature = "/api/" + path + nonce + body
        sigbytes = signature.encode(encoding='UTF-8')
        h = hmac.new(secbytes, sigbytes, hashlib.sha384)
        hexstring = h.hexdigest()
        return {
            "bfx-nonce": nonce,
            "bfx-apikey": self.KEY,
            "bfx-signature": hexstring,
            "content-type": "application/json"
        }

    def req(self, path, params = {}):
        nonce = self._nonce()
        body = params
        rawBody = json.dumps(body)
        headers = self._headers(path, nonce, rawBody)
        url = self.BASE_URL + path
        resp = requests.post(url, headers=headers, data=rawBody, verify=True)
        return resp

    def active_orders(self):
        # Fetch active orders
        response = self.req("v2/auth/r/orders")
        if response.status_code == 200:
          return response.json()
        else:
          print('error, status_code = ', response.status_code)
          return ''

# fetch all your orders and print out
client = BitfinexClient()
result = client.active_orders()
print(result)
票数 10
EN

Stack Overflow用户

发布于 2017-07-09 16:30:43

为什么不使用现有的开源api客户端呢?你可以拿你的工作做比较。https://github.com/scottjbarr/bitfinex https://github.com/tuberculo/bitfinex

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44830995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档