Bitcoin Forum
June 20, 2024, 11:13:38 AM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: python3 mtgox api  (Read 623 times)
DaRude (OP)
Legendary
*
Offline Offline

Activity: 2833
Merit: 1851


In order to dump coins one must have coins


View Profile
April 04, 2013, 07: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")

"Feeeeed me Roger!"  -Bcash
MineMind
Newbie
*
Offline Offline

Activity: 31
Merit: 0



View Profile WWW
April 04, 2013, 08:08:03 AM
 #2

https://github.com/maxme/bitcoin-arbitrage
dflatline
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
April 04, 2013, 08: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?
DaRude (OP)
Legendary
*
Offline Offline

Activity: 2833
Merit: 1851


In order to dump coins one must have coins


View Profile
April 04, 2013, 08: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()))

"Feeeeed me Roger!"  -Bcash
dflatline
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
April 04, 2013, 09: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})
DaRude (OP)
Legendary
*
Offline Offline

Activity: 2833
Merit: 1851


In order to dump coins one must have coins


View Profile
April 04, 2013, 10: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.

"Feeeeed me Roger!"  -Bcash
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!