2017-06-29 12 views
3

新しいv2 APIへの基本認証api呼び出しを作成しようとしていて、無効なapiキーエラーが返されました。bitfinex api v2エラー、無効なキー

同じエラーを確認するために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コールを作成するにはどうすればよいですか?

+0

あなたはPHPのためにそれを必要とするか、またはPHPと比較する:https://stackoverflow.com/a/46851626/2635490 – Phil

答えて

-1

オープンソースのapiクライアントを使用してみませんか?あなたは自分の仕事と比較することができます。 https://github.com/scottjbarr/bitfinex https://github.com/tuberculo/bitfinex

+2

これらはV1のAPIを使用しています。 最新のバージョンに書き込もうとしています。 – NIX

+0

私は実際にはV1接続を持っています。 – NIX

7

ヘッダーが間違っています。私もこれをやろうとしており、bitfinex v2 api docsの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, 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) 
関連する問題