Bitcoin Forum

Other => Beginners & Help => Topic started by: DaRude on April 04, 2013, 07:11:22 AM



Title: python3 mtgox api
Post by: DaRude on April 04, 2013, 07:11:22 AM
Ok so i'm going crazy trying to write a python3 code to get to gox's api v1
i found few programs but they either didn't work for me or are still doing v0 api  ???
when i execute the following code i get
Code:
e.code -> 403
e.read -> b'{"result":"error","error":"Identification required to access private API","token":"login_error_invalid_rest_key"}'

(API keys are correct and are enabled on gox) so if someone can tell me where i'm being an idiot i'd greatly appreciate it.

Code:
import time
import urllib.parse
import urllib.request
import base64
import hmac
import hashlib

url = "https://data.mtgox.com/api/1/generic/info"

GOX_secret = "your secret here"
GOX_secret = base64.b64decode((GOX_secret.encode()))
GOX_key = "your keyname here"

params = {"nonce":str(int(time.time()*1000))}
post_data = urllib.parse.urlencode(params)

#ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),post_data,hashlib.sha512).digest()))
H = hmac.new(GOX_secret, post_data.encode(), hashlib.sha512).digest()
sign = base64.b64encode(H)
header = {"User-Agent": 'myGoxAPI', "Rest-Key": GOX_key, "Rest-Sign": sign}


req = urllib.request.Request(url, post_data, header) #POST

try:
        response = urllib.request.urlopen(req, post_data.encode()) #POST
except urllib.error.HTTPError as e:
        print("e.code ->", e.code)
        print("e.read ->", (e.read()))
        exit(5)

print("It'd be nice to get here")


Title: Re: python3 mtgox api
Post by: MineMind on April 04, 2013, 08:08:03 AM
https://github.com/maxme/bitcoin-arbitrage


Title: Re: python3 mtgox api
Post by: dflatline on April 04, 2013, 08:10:02 AM
I have no idea what I'm talking about (I don't program), but why are you doing this:

GOX_secret = "your secret here"
GOX_secret = base64.b64decode((GOX_secret.encode()))

Why not just define GOX_secret outright?


Title: Re: python3 mtgox api
Post by: DaRude on April 04, 2013, 08:40:49 AM
MineMind, that's python2 and a lot of code for me right now. But i'll start digging.

dflatline, just for readability. I know it looks odd but it's not the same encoding/decoding  When you paste the key from GOX it comes in as a string, so initially GOX_secret.encode() changes a string into a bytes, and then  base64.b64decode changes those bytes to base64 for GOX's API. I guess those two lines could be replaced by GOX_secret = base64.b64decode(("your secret here".encode()))


Title: Re: python3 mtgox api
Post by: dflatline on April 04, 2013, 09:33:19 AM
Why not do:
req = urllib.request.Request(https://data.mtgox.com/api/1/generic/info, data=whatever, headers={"User-Agent": 'myGoxAPI', "Rest-Key": GOX_key, "Rest-Sign": sign})


Title: Re: python3 mtgox api
Post by: DaRude on April 04, 2013, 10:08:12 AM
You can, it'd be the same result just wouldn't be considered a nice style. It's a common practice to declare your variables together at the begging of the code e.g. if the api url changes you only have to change it in one place and don't have to hunt the code looking for every occurrence.