我正在编写一个交易程序,我需要通过API v2连接到MtGox (一种比特币交换)。但我不断地发现以下错误:
网址:1 https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address HTTP错误403:禁止。
我的脚本大部分是这里的直接副本(即pastebin链接)。我只需更改它以使用Python3.3。
我怀疑这与我使用part 64.b64encode的脚本部分有关。在我的代码中,我必须将字符串编码为utf-8才能使用have 64.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
}我想知道这种额外的编码是否导致我的头凭据不正确。我认为这是Python2v.Python3的另一个问题。我不知道另一个人是如何逃脱的,因为如果你试图将一个字符串传递给b64encode或hmac,脚本就不会运行。你们看到我在做什么了吗?外码等效吗?
发布于 2013-04-28 23:11:36
这句话显然是个问题-
ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()).encode('utf-8'))为了澄清,hmac.new()创建了一个对象,然后对其调用摘要()。摘要返回一个字节对象,如
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())发布于 2013-11-12 15:11:35
我相信您可能会在我的一个相关问题上找到帮助,尽管它涉及的是WebSocket API:
在Python3中对MtGox WebSocket API的验证调用
此外,HTTP 403错误似乎表明请求存在根本问题。即使您向API抛出了错误的身份验证信息,您也应该得到一条作为响应的错误消息,而不是403。我的最佳猜测是您使用了错误的HTTP方法,因此检查是否使用了适当的HTTP方法(GET/POST)。
https://stackoverflow.com/questions/16264580
复制相似问题