Skip to content

Instantly share code, notes, and snippets.

@barnabee
Last active April 6, 2022 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnabee/c29ab9b648b96b190ff031327f444e3e to your computer and use it in GitHub Desktop.
Save barnabee/c29ab9b648b96b190ff031327f444e3e to your computer and use it in GitHub Desktop.
Coinbase REST API auth (Python)
import time
import hmac
import hashlib
import requests
import base64
class CoinbaseAuth(requests.auth.AuthBase):
def __init__(self, api_key, secret_key, api_passphrase):
self.api_key = api_key
self.api_passphrase = api_passphrase
self.secret_key = base64.b64decode(secret_key)
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
signature = base64.b64encode(hmac.new(self.secret_key, message.encode(), hashlib.sha256).digest())
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.api_passphrase
})
return request
COINBASE_API = 'https://api.exchange.coinbase.com'
API_KEY = 'xxx'
API_SECRET = 'xxx'
API_PASSPHRASE = 'xxx'
auth = CoinbaseAuth(API_KEY, API_SECRET, API_PASSPHRASE)
r = requests.get(COINBASE_API + '/oracle', auth=auth)
print(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment