Skip to content

Instantly share code, notes, and snippets.

@mardlin
Last active August 30, 2015 22:18
Show Gist options
  • Save mardlin/35a45d3472754cdee4a6 to your computer and use it in GitHub Desktop.
Save mardlin/35a45d3472754cdee4a6 to your computer and use it in GitHub Desktop.
## In response to https://community.coinbase.com/t/exchange-api-order-placement-results-in-a-missing-product-id/6818
import json, hmac, hashlib, time, base64, requests
from requests.auth import AuthBase
# Sandbox credentials and code. Key has all permissions.
API_KEY = ''
API_SECRET = ''
API_PASS = ''
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = (timestamp + request.method + request.path_url + (request.body or '')).encode("utf-8")
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest())
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json',
'Accept': 'application/json'
})
return request
api_url = 'https://api-public.sandbox.exchange.coinbase.com/'
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)
order_dict = {
"price": "500.00",
"size": "0.01",
"side": "buy",
"product_id": "BTC-USD"
}
res = requests.post(api_url + 'orders', data=json.dumps(order_dict), auth=auth)
print res.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment