Coinbase API Python请求语法问题

时间:2018-12-12 23:01:22

标签: python rest authentication coinbase-api

我在使用代码时遇到麻烦,因为每次我运行它时,它都会收到如下错误:     文件“ /Users/richard/Desktop/Python/gdaxauth.py”,第27行     request.headers.update({^     SyntaxError:语法无效

因此,这似乎与request.headers.update部分中的内容有关。我曾尝试对此进行研究,但遇到了总的障碍。任何人都可以建议正确的语法是什么?我正在使用Python 3.7。

import json
import hmac
import hashlib
import time
import requests
import base64
import urllib
from requests.auth import AuthBase

# Tracking execution time
start = time.time()

# Create custom authentication for Exchange
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 '')
        hmac_key = base64.b64decode(self.secret_key)
        signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256)
        signature_b64 = base64.b64encode(signature.digest().rstrip('\n')

        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'
        })
        return request

# Credentials - ADD YOUR API KEY CREDS IN THIS SECTION
API_KEY = "xxxxxxxxx"
SECRET_KEY = "xxxxxxxxxx"
API_PASSPHRASE = "xxxxxxxxxx"

# Get accounts
api_url = 'https://api.gdax.com/' #'https://api.pro.coinbase.com'
auth = CoinbaseExchangeAuth(API_KEY,SECRET_KEY,API_PASSPHRASE)
r = requests.get(api_url + 'accounts', auth=auth)

# Output account data and code execution time
print(json.dumps(r.json(),indent=4))

0 个答案:

没有答案