简体   繁体   English

使用Python连接到MtGox API 2的问题

[英]Problems Connecting to MtGox API 2 with Python

I am writing a trading program that I need to connect to MtGox (a bitcoin exchange) through the API v2. 我正在编写一个交易程序,我需要通过API v2将其连接到MtGox(比特币交易所)。 But I keep getting the following error: 但是我一直收到以下错误:

URL: 1 https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address 网址: 1 https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address

HTTP Error 403: Forbidden. HTTP错误403:禁止。

Most of my script is a direct copy from here (that is a pastebin link). 我的大部分脚本都是从此处直接复制的(即pastebin链接)。 I just had to change it to work with Python 3.3. 我只需要更改它即可与Python 3.3一起使用。

I suspect that it has to do with the part of script where I use base64.b64encode. 我怀疑这与我使用base64.b64encode的脚本部分有关。 In my code , I have to encode my strings to utf-8 to use 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
                }

However, with the other guy's script, he doesn't have too: 但是,根据另一个人的脚本,他也没有:

                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
                }

I'm wondering if that extra encoding is causing my header credentials to be incorrect. 我想知道这种额外的编码是否导致我的标题凭据不正确。 I think this is another Python 2 v. Python 3 problem. 我认为这是另一个Python 2诉Python 3问题。 I don't know how the other guy got away without changing to utf-8, because the script won't run if you try to pass a string to b64encode or hmac. 我不知道另一个人是如何在不更改为utf-8的情况下逃脱的,因为如果您尝试将字符串传递给b64encode或hmac,则脚本将无法运行。 Do you guys see any problems with what I am doing? 你们看到我的工作有任何问题吗? Is out code equivalent? 输出代码是否等效?

This line specifically seems to be the problem - 这行似乎是问题所在-

ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()).encode('utf-8'))

To clarify, hmac.new() creates an object to which you then call digest(). 为了明确起见,hmac.new()创建一个对象,然后将其调用digest()。 Digest returns a bytes object such as 摘要返回一个字节对象,例如

b.digest()

b'\x92b\x129\xdf\t\xbaPPZ\x00.\x96\xf8%\xaa'

Now, when you call str on this, it turns to b'\\\\x92b\\\\x129\\\\xdf\\\\t\\\\xbaPPZ\\\\x00.\\\\x96\\\\xf8%\\\\xaa' 现在,当您对此调用str时,它将变为b'\\\\x92b\\\\x129\\\\xdf\\\\t\\\\xbaPPZ\\\\x00.\\\\x96\\\\xf8%\\\\xaa'

So, see what happens there? 那么,看看那里会发生什么? The byte indicator is now part of the string itself, which you then call encode() on. 字节指示符现在是字符串本身的一部分,然后可以在其上调用encode()

str(b.digest()).encode("utf-8")
b"b'\\x92b\\x129\\xdf\\t\\xbaPPZ\\x00.\\x96\\xf8%\\xaa'"

To fix this, as turning bytes into a string back into bytes was unnecessary anyhow(besides problematic), I believe this will work - 为了解决这个问题,因为无论如何都没有必要将字节转换为字符串再转换为字节(有问题的话),我相信这会起作用-

ahmac = base64.b64encode(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest())

I believe you are likely to find help in a related question of mine although it deals with the WebSocket API: 我相信您可能会在我的一个相关问题中找到帮助,尽管它涉及WebSocket API:
Authenticated call to MtGox WebSocket API in Python 3 在Python 3中对MtGox WebSocket API的经过身份验证的调用

Also, the HTTP 403 error seems to indicate that there is something fundamentally wrong with the request. 同样,HTTP 403错误似乎表明请求根本存在问题。 Even if you threw the wrong authentication info at the API you should have gotten an error message as a response and not a 403. My best guess is that you are using the wrong HTTP method so check if you are using the appropriate one (GET/POST). 即使您在API上输入了错误的身份验证信息,您也应该得到一条错误消息作为响应,而不是403。我的最佳猜测是您使用了错误的HTTP方法,因此请检查您是否使用了适当的方法(GET / POST)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM