2017-10-07 164 views
1

我是python的初學者,但我想從我的私人帳戶在cryptocurrency市場bitbay.net上獲取信息數據。Post API授權

Api description can be found here:

我在Python 3.5的代碼:

import requests 
import json 
import hashlib 
import time 

hash_object = hashlib.sha512(b'public_api_xxxxxx') 
apihash = hash_object.hexdigest()  
timestamp = time.time() 

p = requests.post('https://bitbay.net/API/Trading/tradingApi.php', data={'API-Key':'public_api_xxxxxx','API-Hash':apihash,'Moment':timestamp, 'Method':'info' }) 
p.text 
print(p) 

我花很多時間來解決這個問題,但我仍然得到:

響應[404]

您的援助將非常感謝。爲了獲得最佳答案,我想購買小啤酒:)提前謝謝!

回答

1

要執行hash_mac當量,您可以使用hmac

apihash = hmac.new(secret, data, hashlib.sha512).hexdigest() 

此外,從文檔,API-KeyAPI-Hash是頭。 moment & method場URL編碼在體內

Python2

import requests 
import hashlib 
import hmac 
import time 
import urllib 

secret = "12345" 
apiKey = "public_api_xxxxxx" 

timestamp = int(time.time()) 

data = urllib.urlencode((('method', 'info'),('moment', timestamp))) 

apihash = hmac.new(secret, data, hashlib.sha512).hexdigest() 

res = requests.post('https://bitbay.net/API/Trading/tradingApi.php', 
    headers={ 
    'API-Key':apiKey, 
    'API-Hash' : apihash, 
    'Content-Type' : 'application/x-www-form-urlencoded' 
    }, 
    data=data 
) 

print(res) 
print(res.text) 

Python3

import requests 
import hashlib 
import hmac 
import time 
import urllib 

secret = b"12345" 
apiKey = "public_api_xxxxxx" 

timestamp = int(time.time()) 

data = urllib.parse.urlencode((('method', 'info'),('moment', timestamp))) 

apihash = hmac.new(secret, data.encode('utf-8'), hashlib.sha512).hexdigest() 

res = requests.post('https://bitbay.net/API/Trading/tradingApi.php', 
    headers={ 
    'API-Key':apiKey, 
    'API-Hash' : apihash, 
    'Content-Type' : 'application/x-www-form-urlencoded' 
    }, 
    data=data 
) 

print(res) 
print(res.text) 
+0

感謝您的幫助!你是老闆:)請給我你的cryptocurrency錢包地址 - 我想給你買一杯啤酒:) – Max

+0

不客氣,我很高興能夠幫到你,如果你喜歡它,你可以接受/贊成答案,會很好:) –

+0

好的。謝謝 :) – Max