-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthorization-V2-Example.py
More file actions
44 lines (29 loc) · 1.17 KB
/
Authorization-V2-Example.py
File metadata and controls
44 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from hmac import new
from hashlib import sha256
from time import time
from requests import get
from requests.auth import AuthBase
# https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-key-authentication
# GET https://api.coinbase.com/v2/accounts
api_url = "https://api.coinbase.com/v2/accounts"
api = 'API_KEY' # !!!!!!!
secretkey = 'SECRET_KEY' # !!!!!!!
# Create custom authentication for Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time()))
message = timestamp + request.method + request.path_url + (request.body or '')
signature = new(self.secret_key.encode('utf-8'), message.encode('utf-8'), sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
})
return request
auth = CoinbaseWalletAuth(api, secretkey)
# Get current user
r = get(api_url,auth=auth)
print(r.json() if r.status_code ==200 else (r.status_code, r.text) )