【问题标题】:Post API authorization发布 API 授权
【发布时间】:2017-10-07 23:28:20
【问题描述】:

我是 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]

非常感谢您的帮助。为了获得最佳答案,我想买小啤酒:) 提前谢谢!

【问题讨论】:

    标签: python python-3.x api


    【解决方案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)
    

    【讨论】:

    • 感谢您的帮助!你是老板 :) 请给我你的加密货币钱包地址 - 我想给你买啤酒 :)
    • 不客气,我很高兴能为您提供帮助,如果您愿意,可以接受/投票,这很好:)
    • 好的。谢谢你:)
    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2012-12-23
    • 2019-06-18
    • 2018-06-15
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多