Saturday, April 26, 2014

Actual Coinbase Client HTTP (Get & Post)

I like coinbase, seems like a pretty robust API, but sadly there are documentation bugs and they arent so hot at replying to support issues. After some thinking I figured out why the API calls were thinking they were not JSON.
# there is a problem with the coinbase python example
# at https://coinbase.com/docs/api/authentication where they
# fail to add the content type header to the POST action
# this is the actual implementation
def get_http(self, url, body=None):
opener = urllib2.build_opener()
nonce = int(time.time() * 1e6)
message = str(nonce) + url + ('' if body is None else body)
signature = hmac.new(settings.COINBASE_API_SECRET, message, hashlib.sha256).hexdigest()
opener.addheaders = [('ACCESS_KEY', settings.COINBASE_API_KEY),
('ACCESS_SIGNATURE', signature),
('ACCESS_NONCE', nonce)]
try:
response = opener.open(urllib2.Request(url,body,{'Content-Type': 'application/json'}))
return response
except urllib2.HTTPError as e:
print e
return e

No comments: