Created
February 5, 2018 23:08
-
-
Save alferz/4ff1433ae43d15b2b4a26c51a3ec569c to your computer and use it in GitHub Desktop.
Python Coinbase API with API-Key Auth Example (SHA256 HMAC Signature)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getCoinbaseData(path): | |
import urllib2, json | |
import hmac | |
import hashlib | |
import base64 | |
import time | |
apiKey = '<YOUR API KEY>' | |
apiSecret = '<YOUR API SECRET>' | |
url = "https://api.coinbase.com%s" % path | |
headers = {} | |
timestamp = int(time.time()) | |
headers['CB-ACCESS-KEY'] = apiKey | |
headers['CB-ACCESS-TIMESTAMP'] = timestamp | |
headers['CB-VERSION'] = '2018-02-01' | |
toSign = str(timestamp) + 'GET' + path | |
signature = hmac.new(apiSecret, toSign, hashlib.sha256).hexdigest() | |
headers['CB-ACCESS-SIGN'] = signature | |
try: | |
request = urllib2.Request(url, headers=headers) | |
response = urllib2.urlopen(request, timeout=2.5) | |
data = json.loads(response.read()) | |
print data | |
except Exception as e: | |
print "ERROR: %s" % str(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment