2017-05-26 76 views
-1

我知道这听起来像我正在跳枪问这个。然而,我几个小时以来一直在抨击我的头脑。我试图从BTC-e得到响应,我有一个工作python2版本,只是在python3失败。我尝试自动转换它,查找包名称更改等只是死在我身上。这里是工作python2样本:(不是我的代码)HMAC的Python 2到3转换

#! /usr/bin/python 
import httplib 
import urllib 
import json 
import hashlib 
import hmac 
from auth import Ekey, Esecret 

# Replace these with your own API key data 
BTC_api_key = Ekey 
BTC_api_secret = Esecret 
# Come up with your own method for choosing an incrementing nonce 
def generate_nonce(length=8): 
"""Generate pseudorandom number.""" 
return ''.join([str(random.randint(0, 9)) for i in range(length)]) 

nonce = generate_nonce() 

# method name and nonce go into the POST parameters 
params = {"method":"getInfo", 
      "nonce": nonce} 
params = urllib.urlencode(params) 

# Hash the params string to produce the Sign header value 
H = hmac.new(BTC_api_secret, digestmod=hashlib.sha512) 
H.update(params) 
sign = H.hexdigest() 

headers = {"Content-type": "application/x-www-form-urlencoded", 
        "Key":BTC_api_key, 
        "Sign":sign} 
conn = httplib.HTTPSConnection("btc-e.com") 
conn.request("POST", "/tapi", params, headers) 
response = conn.getresponse() 

print response.status, response.reason 
print json.load(response) 

conn.close() 

然后不幸的堆栈溢出药汁我从什么地方,它返回无效的签名黄牛。

#! /usr/bin/python3 
from time import time 
import urllib.parse 
import hashlib 
import hmac 
import requests 
import json 
APIKey = b'key-key-key' 
secret = b'secret' 
url = "https://btc-e.com/tapi" 

payload = { 
    'method': 'getInfo', 
    'nonce': int(time() * 1000), 
} 

paybytes = urllib.parse.urlencode(payload).encode() 
print(paybytes) 

sign = hmac.new(secret, paybytes, digestmod=hashlib.sha512).hexdigest() 
print(sign) 

headers = { 
    "Content-type": "application/x-www-form-urlencoded", 
    'Key':APIKey, 
    'Sign': sign 
} 
r = requests.post(url, headers=headers, data=paybytes) 
result = r.json() 
print(result) 

他/她们为什么不同?对于api文档,我试图使用go here

+0

大得多你可能会得到下投票,因为你还没有提供[MCVE],这是我们的调试能力至关重要的码。请更新您的问题以包含一个“为什么我的代码不工作”需要**明确的问题陈述**。你可能想看看[问]。 –

+0

@JasonBasanese:你的问题并不坏,我不明白为什么它被低估。如果它需要私人API密钥,那么提供一个工作示例有点困难。 – Blender

+0

我可能会对它进行编辑以使明日更有意义。 –

回答

3

您的随机数太大。从该网站的API docs您链接到:

最小的随机数值 - 1,最大 - 4294967294

当前时间戳是1495778924773,比4294967294

+0

谢谢,我现在就试试。 –

+1

就是这样......它总是那些小东西。 –