本文介绍了bitfinex api v2 错误,无效密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对其新的 v2 api 进行基本的经过身份验证的 api 调用,并返回无效的 api 密钥错误.

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

 from time 导入时间导入 urllib.request导入 urllib.parse导入哈希库进口hmacAPIkey = b'myapikeyyouarenotsupposedtosee'秘密 = b'myceeeeeecretkeyyyy'url = 'https://api.bitfinex.com/v2/auth/r/wallets'有效载荷 = {#'请求':'/auth/r/wallets','nonce': int(time() * 1000),}paybytes = urllib.parse.urlencode(payload).encode('utf8')打印(paybytes)sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()打印(签名)标题 = {'密钥':APIkey,'签':签到}req = urllib.request.Request(url, headers=headers, data=paybytes)使用 urllib.request.urlopen(req) 作为响应:the_page = response.read()打印(the_page)

如何对用于 bitfinex 的新 v2 API 进行经过身份验证的 api 调用?

解决方案

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

## 示例 Bitfinex API v2 Auth Python 代码#导入请求 # pip 安装请求导入json导入 base64导入哈希库进口hmac导入操作系统导入时间#for nonce类 BitfinexClient(对象):BASE_URL = "https://api.bitfinex.com/"KEY = "API_KEY_HERE"SECRET = "API_SECRET_HERE"def _nonce(self):# 返回一个随机数# 用于身份验证返回 str(int(round(time.time() * 10000)))def _headers(self, path, nonce, body):secbytes = self.SECRET.encode(encoding='UTF-8')签名 = "/api/" + 路径 + 随机数 + 正文sigbytes = signature.encode(encoding='UTF-8')h = hmac.new(secbytes, sigbytes, hashlib.sha384)hexstring = h.hexdigest()返回 {bfx-nonce":现时,bfx-apikey":self.KEY,bfx-signature":十六进制字符串,内容类型":应用程序/json"}def req(self, path, params = {}):nonce = self._nonce()正文 = 参数rawBody = json.dumps(body)headers = self._headers(path, nonce, rawBody)url = self.BASE_URL + 路径resp = requests.post(url, headers=headers, data=rawBody, verify=True)返回响应def active_orders(self):# 获取活跃订单response = self.req("v2/auth/r/orders")如果 response.status_code == 200:返回 response.json()别的:打印('错误,status_code = ',response.status_code)返回 '​​'# 获取你所有的订单并打印出来客户端 = BitfinexClient()结果 = client.active_orders()打印(结果)

I am trying to make a basic authenticated api call to their new v2 api and getting an invalid api key error returned.

I reissued the api key just to verify, same error.

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)

How do I make an authenticated api call to the new v2 API for bitfinex?

解决方案

Your headers are wrong. I was also trying to do this and tried using the example code from the bitfinex v2 api docs, however their example contained a bug in that they needed to encode the strings into UTF-8 byte arrays first. So I've fixed it and posting the entire example below.

#
# 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)

这篇关于bitfinex api v2 错误,无效密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 23:24