Bitcoin Forum
November 09, 2024, 04:56:07 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: Req: Python MtGox auth example  (Read 14550 times)
osmosis (OP)
Sr. Member
****
Offline Offline

Activity: 300
Merit: 250



View Profile
October 25, 2011, 06:37:56 AM
 #1

The wiki has a mtgox authentication example in PHP. Anyone out there have an example they can provide for python?  I have been trying to get goxsh.py to work with no luck.

https://en.bitcoin.it/wiki/MtGox/API
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
October 25, 2011, 06:52:50 AM
 #2

Code:
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
osmosis (OP)
Sr. Member
****
Offline Offline

Activity: 300
Merit: 250



View Profile
October 26, 2011, 05:36:27 AM
 #3

THANKS!
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
October 26, 2011, 07:42:56 AM
 #4

THANKS!
please consider to donate, so i am motivated to helping more people. Smiley

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
molecular
Donator
Legendary
*
Offline Offline

Activity: 2772
Merit: 1019



View Profile
January 10, 2012, 07:01:17 PM
 #5

Code:
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

cool, this helped speed things up, thanks.

Donation address?

PGP key molecular F9B70769 fingerprint 9CDD C0D3 20F8 279F 6BE0  3F39 FC49 2362 F9B7 0769
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
January 10, 2012, 07:22:37 PM
 #6

cool, this helped speed things up, thanks.

Donation address?
1JpsKmkim7REYq2EWE5aEXG8YN4zJTTX16

thanks Smiley

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
osmosis (OP)
Sr. Member
****
Offline Offline

Activity: 300
Merit: 250



View Profile
January 10, 2012, 07:27:13 PM
 #7

THANKS!
please consider to donate, so i am motivated to helping more people. Smiley

thx kokjo
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
January 10, 2012, 07:34:28 PM
 #8

im amazed it actually works. it was something i wrote up, in like 10 min.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
molecular
Donator
Legendary
*
Offline Offline

Activity: 2772
Merit: 1019



View Profile
January 11, 2012, 02:51:52 AM
 #9

I'm also amazed.

looked at goxsh python impl a while back and I couldn't even figure out wtf was going on...

your solution: drop 2 functions into my sources, change 2-3 lines => switched from user/pass to api key auth.

thanks kokjo, sent a nickle

PGP key molecular F9B70769 fingerprint 9CDD C0D3 20F8 279F 6BE0  3F39 FC49 2362 F9B7 0769
finway
Hero Member
*****
Offline Offline

Activity: 714
Merit: 500


View Profile
January 11, 2012, 05:58:22 AM
 #10

mark.

Ente
Legendary
*
Offline Offline

Activity: 2126
Merit: 1001



View Profile
January 28, 2012, 12:20:40 PM
 #11

Code:
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

Thank you a lot for posting this!

Uh, python newbie question:
How to use that class?
I tried:

Quote
key = "12345"
secret = "1234567890"
path = "info.php"
args = "123"

tuer = requester(key,secret)
tuer.perform(path,args)
(key and secret replaced)

Dropped error:
Quote
Traceback (most recent call last):
  File "./auth.py", line 48, in <module>
    tuer.perform(path,args)
  File "./auth.py", line 33, in perform
    data, headers = self.build_query(args)
  File "./auth.py", line 24, in build_query
    req["nonce"] = get_nonce()
TypeError: 'str' object does not support item assignment

By the way, what goes in the "args" when none are needed, like when fetching "info"?

Ah, success comes in tiny, bloody steps..

Ente
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
January 28, 2012, 12:44:58 PM
 #12

Code:
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

Thank you a lot for posting this!

Uh, python newbie question:
How to use that class?
I tried:

Quote
key = "12345"
secret = "1234567890"
path = "info.php"
args = "123"

tuer = requester(key,secret)
tuer.perform(path,args)
(key and secret replaced)

Dropped error:
Quote
Traceback (most recent call last):
  File "./auth.py", line 48, in <module>
    tuer.perform(path,args)
  File "./auth.py", line 33, in perform
    data, headers = self.build_query(args)
  File "./auth.py", line 24, in build_query
    req["nonce"] = get_nonce()
TypeError: 'str' object does not support item assignment

By the way, what goes in the "args" when none are needed, like when fetching "info"?

Ah, success comes in tiny, bloody steps..

Ente
args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
Ente
Legendary
*
Offline Offline

Activity: 2126
Merit: 1001



View Profile
January 28, 2012, 01:31:37 PM
 #13

args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work.

Haha I really an a noob! :-)
Thank you, kokjo!

*flips a coin in your direction*

Ente
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
January 28, 2012, 01:33:03 PM
 #14

args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work.

Haha I really an a noob! :-)
Thank you, kokjo!

*flips a coin in your direction*

Ente
thank you.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
molecular
Donator
Legendary
*
Offline Offline

Activity: 2772
Merit: 1019



View Profile
January 28, 2012, 09:01:14 PM
 #15

args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work.

As much as I like python, but one has to admit that languages that use static typing do have certain advantages.

PGP key molecular F9B70769 fingerprint 9CDD C0D3 20F8 279F 6BE0  3F39 FC49 2362 F9B7 0769
Ente
Legendary
*
Offline Offline

Activity: 2126
Merit: 1001



View Profile
January 31, 2012, 01:46:42 PM
 #16

ah, here comes my next stumbling block:
(still Python, still with the code kokjo was so nice to post)

How do I cancel an order?
I only found old-api-examples (the apt where you POST your credentials directly).
Is it even possible with the newer api?

It expects:
#POST data: oid=#&type=#
#oid: Order ID
#type: 1 for sell order or 2 for buy order

For example:
OID=123asd-123-etz-1542-12947
Type=1

123asd-123-etz-1542-12947=1

How do I pass this string into the "args" variable?

Stupid.. everything else (get info, get orders, buy, sell) works as simple as passing the arguments as a JSON to the .php.
Do I have to rewrite it all just for this single "cancel" order? Or is it only possible with the old API altogether, which will be deactivated in some weeks?


..being noobish again:

Ente

edit:
I would expect something as easy as this to work:

Code:
path_cancel = "0/cancelOrder.php"
oid = "123-124-124-124"
typ = "1"
args_cancel = {oid: typ}
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
January 31, 2012, 02:03:03 PM
 #17

Code:
args = {"oid": "123-456-bla-bla", "type":"1"}

notice the "" around the 1, AFAIK does urlencode not take numbers, only strings.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
Ente
Legendary
*
Offline Offline

Activity: 2126
Merit: 1001



View Profile
January 31, 2012, 02:12:38 PM
 #18

Yea, works, great! :-)
Actually I had the "1" in "", my mistake was even more silly.

I did:
Code:
{"123-456-bla-bla": "1"}

expected is:
Code:
{"oid": "123-456-bla-bla", "type":"1"}

Thanks mate, again!

Ente

edit:
wiki says
Code:
oid=#&type=# 
.
I just read that wrong. This is the moment where I really sense the value in programming languages. What the speaker says is what the listener will understand. Without two "but I meant.." in between!
DBordello
Sr. Member
****
Offline Offline

Activity: 349
Merit: 250


BTCPak.com - Exchange your Bitcoins for MP!


View Profile WWW
February 10, 2012, 01:25:19 AM
 #19

I am having difficulty getting this to work.  Here is the code I am using.  Any thoughts?

I am getting not logged in errors.



Code:
def test():
key = 'x'
secret = 'x'
path = "info.php"
args = {}

tuer = requester(key,secret)
r = tuer.perform(path,args)
print r

#### MT Gox Code ####
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

www.BTCPak.com - Exchange your bitcoins for MP: Secure, Anonymous and Easy!
Waam
Newbie
*
Offline Offline

Activity: 21
Merit: 0


View Profile
February 19, 2012, 05:17:06 AM
 #20

I am having difficulty getting this to work.  Here is the code I am using.  Any thoughts?

I am getting not logged in errors.



Code:
def test():
key = 'x'
secret = 'x'
path = "info.php"
args = {}

tuer = requester(key,secret)
r = tuer.perform(path,args)
print r

#### MT Gox Code ####
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
    return int(time.time()*100000)

def sign_data(secret, data):
    return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
     
class requester:
    def __init__(self, auth_key, auth_secret):
        self.auth_key = auth_key
        self.auth_secret = base64.b64decode(auth_secret)
       
    def build_query(self, req={}):
        req["nonce"] = get_nonce()
        post_data = urlencode(req)
        headers = {}
        headers["User-Agent"] = "GoxApi"
        headers["Rest-Key"] = self.auth_key
        headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
        return (post_data, headers)
       
    def perform(self, path, args):
        data, headers = self.build_query(args)
        req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
        res = urllib2.urlopen(req, data)
        return json.load(res)

key =, etc are variables -- They need to be declared outside of a function. imports/froms should be before any other syntax.

Let me know if you need any more help and I can post my working script (up to Authentication, I wouldn't want to do the work for ya.) Wink
Pages: [1] 2 »  All
  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!