【问题标题】:Coinbase Advanced trade API connectivity using python3 is not working使用 python3 的 Coinbase 高级交易 API 连接不起作用
【发布时间】:2023-02-17 06:53:35
【问题描述】:

我根据 coinbase 文档 coinbase doc 尝试了以下代码 该文档是为 Python2 提供的,但我已经修改并将其用于 Python3,因为我正在尝试连接到 Coinbase 中的高级交易 API Coinbase Advanced trade doc

import datetime
import time
import hmac
import hashlib
import http.client


secret_key='***'    #hidden
api_key='***'       #hidden

date_time = datetime.datetime.utcnow()
timestamp=int(time.mktime(date_time.timetuple())) # timestamp should be from UTC time and no decimal allowed

method = "GET" # method can be GET or POST. Only capital is allowed
request_path = 'api/v3/brokerage/accounts'
body=''
message= str(timestamp) + method + request_path + body
signature = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()


headers={
'accept':'application/json',
'CB-ACCESS-KEY': api_key,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-SIGN': signature
}


conn = http.client.HTTPSConnection("api.coinbase.com")
payload = ''

conn.request("GET", "/api/v3/brokerage/accounts", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

执行此代码时,我期待帐户详细信息。但我越来越未经授权的错误API 返回错误代码 401。

我之前能够连接到 Coinbase Pro API,一切都很好,直到 coinbase 和 Coinbase Pro 合并。现在无法弄清楚如何连接到 coinbase 中的高级交易功能。

【问题讨论】:

  • 欢迎来到堆栈溢出!请编辑您的问题以在问题正文中包含错误输出。这比包含图像更可取,尤其是分辨率如此低且缺乏上下文的图像。
  • 我删除了图像,突出显示了错误消息并添加了错误代码。

标签: python-3.x cryptocurrency coinbase-api


【解决方案1】:

好的,所以我进行了两次更新以使其运行。

  1. request_path = '/api/v3/brokerage/accounts' 中的 api 之前插入一个 / :)

  2. 将生成 timestamp 的方式更改为 timestamp = str(int(time.time()))

    我认为将您的 timestamp 更新为一个字符串可以修复它,但它没有,所以我恢复了生成它的方式。也许有人可以告诉我们为什么一个有效而另一个无效。如果我弄明白了,我一定会更新这篇文章。

    这是完整的工作代码。我保持其他一切不变,但用我的替换了你的 cmets。

    import datetime
    import time
    import hmac
    import hashlib
    import http.client
    
    secret_key = '***'  
    api_key = '***'  
    
    # This is the first part where you were getting time
    #date_time = datetime.datetime.utcnow() 
    
    # And this is the second part where you format it as an integer
    #timestamp=int(time.mktime(date_time.timetuple())) 
    
    # I cast your timestamp as a string, but it still doesn't work, and I'm having a hard time figuring out why.
    #timestamp=str(int(time.mktime(date_time.timetuple()))) 
    
    # So I reverted to the way that I'm getting the timestamp, and it works
    timestamp = str(int(time.time()))
    
    method = "GET"
    request_path = '/api/v3/brokerage/accounts' # Added a forward slash before 'api'
    body=''
    message= str(timestamp) + method + request_path + body
    signature = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
    
    
    headers={
    'accept':'application/json',
    'CB-ACCESS-KEY': api_key,
    'CB-ACCESS-TIMESTAMP': timestamp,
    'CB-ACCESS-SIGN': signature
    }
    
    
    conn = http.client.HTTPSConnection("api.coinbase.com")
    payload = ''
    
    conn.request("GET", "/api/v3/brokerage/accounts", payload, headers)
    # You were probably troubleshooting, but the above line is redundant and can be simplified to:
    # conn.request("GET", request_path, body, headers) 
    
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    

【讨论】:

    【解决方案2】:

    我看到有人为 Advanced Trade 端点开源了一个 python 库:https://github.com/KmiQ/coinbase-advanced-python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 2019-01-09
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多