Pages:
Author

Topic: Req: Python MtGox auth example - page 2. (Read 14543 times)

legendary
Activity: 2126
Merit: 1001
January 31, 2012, 09:46:42 AM
#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}
donator
Activity: 2772
Merit: 1019
January 28, 2012, 05: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.
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 28, 2012, 09:33:03 AM
#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.
legendary
Activity: 2126
Merit: 1001
January 28, 2012, 09:31:37 AM
#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
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 28, 2012, 08:44:58 AM
#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
    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.
legendary
Activity: 2126
Merit: 1001
January 28, 2012, 08:20:40 AM
#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
    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
hero member
Activity: 714
Merit: 500
January 11, 2012, 01:58:22 AM
#10
mark.
donator
Activity: 2772
Merit: 1019
January 10, 2012, 10:51:52 PM
#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
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 10, 2012, 03:34:28 PM
#8
im amazed it actually works. it was something i wrote up, in like 10 min.
sr. member
Activity: 300
Merit: 250
January 10, 2012, 03:27:13 PM
#7
THANKS!
please consider to donate, so i am motivated to helping more people. Smiley

thx kokjo
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 10, 2012, 03:22:37 PM
#6
cool, this helped speed things up, thanks.

Donation address?
1JpsKmkim7REYq2EWE5aEXG8YN4zJTTX16

thanks Smiley
donator
Activity: 2772
Merit: 1019
January 10, 2012, 03: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?
legendary
Activity: 1050
Merit: 1000
You are WRONG!
October 26, 2011, 03:42:56 AM
#4
THANKS!
please consider to donate, so i am motivated to helping more people. Smiley
sr. member
Activity: 300
Merit: 250
October 26, 2011, 01:36:27 AM
#3
THANKS!
legendary
Activity: 1050
Merit: 1000
You are WRONG!
October 25, 2011, 02: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)
sr. member
Activity: 300
Merit: 250
October 25, 2011, 02: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
Pages:
Jump to: