Author

Topic: python3 mtgox api (Read 623 times)

legendary
Activity: 2772
Merit: 1786
In order to dump coins one must have coins
April 04, 2013, 06:08:12 AM
#6
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.
newbie
Activity: 14
Merit: 0
April 04, 2013, 05:33:19 AM
#5
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})
legendary
Activity: 2772
Merit: 1786
In order to dump coins one must have coins
April 04, 2013, 04:40:49 AM
#4
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()))
newbie
Activity: 14
Merit: 0
April 04, 2013, 04:10:02 AM
#3
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?
newbie
Activity: 31
Merit: 0
legendary
Activity: 2772
Merit: 1786
In order to dump coins one must have coins
April 04, 2013, 03:11:22 AM
#1
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  Huh
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")
Jump to: