发布API授权

时间:2017-10-07 23:28:21

标签: python python-3.x api

我是python的初学者,但我想从加密货币市场bitbay.net上的私人帐户获取信息数据。

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 个答案:

答案 0 :(得分:1)

要执行等效的hash_mac,您可以使用hmac

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

此外,从文档中,API-KeyAPI-Hash是标题。 moment& method字段在正文中进行了网址编码

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)