Hi. I have been trying to follow the unofficial documentation for 2 days without success. What frastrate me is even the simpliest case directly taken from example code doesn't work.
The first example code is, forgive me putting up all:
import hmac, base64, hashlib, urllib2
base = 'https://data.mtgox.com/api/2/'
def makereq(key, secret, path, data):
hash_data = path + chr(0) + data
secret = base64.b64decode(secret)
sha512 = hashlib.sha512
hmac = str(hmac.new(secret, hash_data, sha512))
header = {
'User-Agent': 'My-First-Trade-Bot',
'Rest-Key': key,
'Rest-Sign': base64.b64encode(hmac),
'Accept-encoding': 'GZIP',
'Content-Type': 'application/x-www-form-urlencoded'
}
return urllib2.Request(base + path, data, header)
post_data = 'nonce=123'
request = makreq('abc123..', 'aBc7/+..', 'BTCUSD/money/ticker', post_data)
response = urllib2.urlopen(request, post_data)
# if gzip encoding, decode
# try to decode json into dictionary
# raise exception if response contains error key
And it fail out of the box:
$ python2.7 test.py
Traceback (most recent call last):
File "test.py", line 23, in
request = makereq(key, secret, 'BTCUSD/money/ticker', post_data)
File "test.py", line 8, in makereq
hmac = str(hmac.new(secret, hash_data, sha512))
UnboundLocalError: local variable 'hmac' referenced before assignment
I "fix" it by renaming the variable hmac to hmac2, and I get:
$ python2.7 test.py
Traceback (most recent call last):
File "test.py", line 24, in
response = urllib2.urlopen(request, post_data)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
This is really clueless, if the server just give an 5xxx without any detail. The key/secret used in my case works fine on API_1, and I tried again by applying a new key/secret, which failed with exactly the same 500.
Do you have a code snippet of minimal working code to access an authenticated-user-only feature? E.g. to access wallet information would be perfect. I usually use python3 and am happy to morph a piece of working python2 code to a piece of working python3 code.
The documentation is a rather big topic, worth making up a mailing list or separate board. I feel guilty to make this disordered thread even longer.
The next example I can look up is this:
http://pastebin.com/aXQfULyqWhich also stopped working as soon as I need to do something that requires authentication. From their source it seems they don't even encrypt post data as part of their Sign, thus must fail authentication -> only work for public accessible URIs.