【问题标题】:Translating PHP to Python (Rest-API connection) [closed]将 PHP 转换为 Python(Rest-API 连接)[关闭]
【发布时间】:2012-12-03 08:21:50
【问题描述】:

我正在学习 Python,作为练习,我尝试编写一个程序来在比特币市场上进行交易:https://bitcurex.com。这是一个 API 参考:https://bitcurex.com/reading-room/API。有一个PHP客户端示例,所以我尝试将它翻译成Python,所以我得到了:

import math
import time
import simplejson
import urllib
import urllib2
import hmac,hashlib

def microtime():
    return '%f %d' % math.modf(time.time())

def query( path, key, secret, data={} ):
    mt = microtime().split()
    nonce = mt[1] + mt[0][2:]
    data['nonce'] = nonce

    post_data = urllib.urlencode( data )

    sign = hmac.new( secret.decode('base64'), post_data, hashlib.sha512 ).digest()

    headers = {'Rest-Key' : key,
               'Rest-Sign': sign.encode('base64').strip(),
               'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
               'Content-type': 'application/x-www-form-urlencoded'}
    print headers

    url = 'https://bitcurex.com/api/0/' + path

    req = urllib2.Request( url, post_data, headers )
    response = urllib2.urlopen(req)

    return simplejson.loads(response.read())

print query('getFunds', '29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09',  'y2NDxKGa/xvhtXrDP+3oscbBUFSac9+T8jzu2nRmt0vBdHbbl8NRqdmxKFr2IwwY5LAskTQZGyy2XONaNN6Jrg==')

这些 API 密钥有效 - 您只能使用它们进行 getFunds 查询。

它不断返回错误“我必须登录”。我尝试通过 Fiddler Proxy Debugger 查看该请求,这里有该尝试的标题:

POST /api/0/getFunds HTTP/1.1
Accept-Encoding: identity
Rest-Sign: Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H
WIc7bH56sQ==: 
Content-Length: 22
Rest-Key: 29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09
Connection: close
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)
Host: bitcurex.com
Content-Type: application/x-www-form-urlencoded

Fiddler 显示错误:

Incorrectly formed request headers.
Missing colon in header #3, WIc7bH56sQ==

有什么想法吗?好像我的 Rest-Sign 太长或类似的东西。我认为我的代码应该与 PHP 示例完全相同。我做错了什么?

【问题讨论】:

    标签: php python python-2.7 bitcoin


    【解决方案1】:

    这行很可疑:

    'Rest-Sign': sign.encode('base64').strip()
    

    您真的希望标题的值包含文字“\n”符号吗?这是 encode('base64') 返回的——在你的例子中,这个字符串:

    'Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H\nWIc7bH56sQ=='
    

    注意中间的\n。我不确定,但可能删除所有 \n 标志可以满足您的需求。

    【讨论】:

    • 也为我工作,但每次我得到 403 响应。我使用了自己的密钥/秘密值,但仍然无法登录
    猜你喜欢
    • 2023-04-08
    • 2017-10-11
    • 1970-01-01
    • 2011-05-06
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2011-01-15
    相关资源
    最近更新 更多