Bitcoin Forum
December 03, 2016, 03:44:16 PM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Improved Python code for MTGox API (HTTP auth)  (Read 1579 times)
knite
Jr. Member
*
Offline Offline

Activity: 33


View Profile
March 29, 2012, 01:06:27 AM
 #1

I found this thread to be extremely helpful, so I'm returning the favor by posting an improved and simplified version of the code. Unfortunately, my forum account is limited to the Newbie section. Mods, please move this post to the thread above.
Code:
from hashlib import sha512
from hmac import HMAC
from base64 import b64decode, b64encode
from urllib import urlencode

from time import time

import requests

class MTGox:
def __init__(self, key, secret, base):
self.key = key
self.secret = b64decode(secret)
self.base = base

def query(self, path, args={}):
args['nonce'] = int(time()*100000)
headers = {
'User-Agent': 'BitcoinTalk',
'Rest-Key': self.key,
'Rest-Sign': b64encode(str(HMAC(self.secret, urlencode(args), sha512).digest()))
}
r = requests.post(self.base + path, data=args, headers=headers)
print r.text
1480779856
Hero Member
*
Offline Offline

Posts: 1480779856

View Profile Personal Message (Offline)

Ignore
1480779856
Reply with quote  #2

1480779856
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1480779856
Hero Member
*
Offline Offline

Posts: 1480779856

View Profile Personal Message (Offline)

Ignore
1480779856
Reply with quote  #2

1480779856
Report to moderator
1480779856
Hero Member
*
Offline Offline

Posts: 1480779856

View Profile Personal Message (Offline)

Ignore
1480779856
Reply with quote  #2

1480779856
Report to moderator
1480779856
Hero Member
*
Offline Offline

Posts: 1480779856

View Profile Personal Message (Offline)

Ignore
1480779856
Reply with quote  #2

1480779856
Report to moderator
TheStig
Member
**
Offline Offline

Activity: 105



View Profile
March 29, 2012, 01:21:44 AM
 #2

I posted it in that thread for you.

Ente
Legendary
*
Offline Offline

Activity: 1834



View Profile
March 29, 2012, 07:46:22 AM
 #3

Thank you, both!

Your code is for using the old http api to do commands?
I ask since now you can use the (more or less) same commands over the streaming api too..

I may find you the thread for this if interested.

Ente
knite
Jr. Member
*
Offline Offline

Activity: 33


View Profile
March 29, 2012, 04:00:21 PM
 #4

Your code is for using the old http api to do commands?
I ask since now you can use the (more or less) same commands over the streaming api too..

Yes, this is for the HTTP API. It's much simpler to implement than the streaming API. It's also good to have a backup, as reports suggest the streaming API is somewhat unstable. I'm rewriting the streaming code from here as well.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!