Bitcoin Forum
May 07, 2024, 05:10:32 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Improved Python code for MTGox API (HTTP auth)  (Read 1734 times)
knite (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


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
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715101832
Hero Member
*
Offline Offline

Posts: 1715101832

View Profile Personal Message (Offline)

Ignore
1715101832
Reply with quote  #2

1715101832
Report to moderator
1715101832
Hero Member
*
Offline Offline

Posts: 1715101832

View Profile Personal Message (Offline)

Ignore
1715101832
Reply with quote  #2

1715101832
Report to moderator
1715101832
Hero Member
*
Offline Offline

Posts: 1715101832

View Profile Personal Message (Offline)

Ignore
1715101832
Reply with quote  #2

1715101832
Report to moderator
TheStig
Member
**
Offline Offline

Activity: 105
Merit: 10



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

I posted it in that thread for you.

Ente
Legendary
*
Offline Offline

Activity: 2126
Merit: 1001



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 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


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:  

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