加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – bitfinex api v2错误,无效密钥

发布时间:2020-12-20 12:01:21 所属栏目:Python 来源:网络整理
导读:我正在尝试对他们的新v2 api进行基本的身份验证api调用,并返回无效的api密钥错误. 我重新发布api密钥只是为了验证,同样的错误. from time import timeimport urllib.requestimport urllib.parseimport hashlibimport hmacAPIkey = b'myapikeyyouarenotsuppos
我正在尝试对他们的新v2 api进行基本的身份验证api调用,并返回无效的api密钥错误.

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

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 API进行经过身份验证的api调用?

解决方法

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

#
# 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,params = {}):
        nonce = self._nonce()
        body = params
        rawBody = json.dumps(body)
        headers = self._headers(path,rawBody)
        url = self.BASE_URL + path
        resp = requests.post(url,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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读