[英]Problems Connecting to MtGox API 2 with Python
我正在编写一个交易程序,我需要通过API v2将其连接到MtGox(比特币交易所)。 但是我一直收到以下错误:
网址: 1 https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address
HTTP错误403:禁止。
我的大部分脚本都是从此处直接复制的(即pastebin链接)。 我只需要更改它即可与Python 3.3一起使用。
我怀疑这与我使用base64.b64encode的脚本部分有关。 在我的代码中 ,我必须将字符串编码为utf-8才能使用base64.b64encode:
url = self.__url_parts + '2/' + path
api2postdatatohash = (path + chr(0) + post_data).encode('utf-8') #new way to hash for API 2, includes path + NUL
ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()).encode('utf-8'))
# Create header for auth-requiring operations
header = {
"User-Agent": 'Arbitrater',
"Rest-Key": self.key,
"Rest-Sign": ahmac
}
但是,根据另一个人的脚本,他也没有:
url = self.__url_parts + '2/' + path
api2postdatatohash = path + chr(0) + post_data #new way to hash for API 2, includes path + NUL
ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()))
# Create header for auth-requiring operations
header = {
"User-Agent": 'genBTC-bot',
"Rest-Key": self.key,
"Rest-Sign": ahmac
}
我想知道这种额外的编码是否导致我的标题凭据不正确。 我认为这是另一个Python 2诉Python 3问题。 我不知道另一个人是如何在不更改为utf-8的情况下逃脱的,因为如果您尝试将字符串传递给b64encode或hmac,则脚本将无法运行。 你们看到我的工作有任何问题吗? 输出代码是否等效?
这行似乎是问题所在-
ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()).encode('utf-8'))
为了明确起见,hmac.new()创建一个对象,然后将其调用digest()。 摘要返回一个字节对象,例如
b.digest()
b'\x92b\x129\xdf\t\xbaPPZ\x00.\x96\xf8%\xaa'
现在,当您对此调用str时,它将变为b'\\\\x92b\\\\x129\\\\xdf\\\\t\\\\xbaPPZ\\\\x00.\\\\x96\\\\xf8%\\\\xaa'
那么,看看那里会发生什么? 字节指示符现在是字符串本身的一部分,然后可以在其上调用encode()
。
str(b.digest()).encode("utf-8")
b"b'\\x92b\\x129\\xdf\\t\\xbaPPZ\\x00.\\x96\\xf8%\\xaa'"
为了解决这个问题,因为无论如何都没有必要将字节转换为字符串再转换为字节(有问题的话),我相信这会起作用-
ahmac = base64.b64encode(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest())
我相信您可能会在我的一个相关问题中找到帮助,尽管它涉及WebSocket API:
在Python 3中对MtGox WebSocket API的经过身份验证的调用
同样,HTTP 403错误似乎表明请求根本存在问题。 即使您在API上输入了错误的身份验证信息,您也应该得到一条错误消息作为响应,而不是403。我的最佳猜测是您使用了错误的HTTP方法,因此请检查您是否使用了适当的方法(GET / POST)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.