[英]Coinbase Advanced trade API connectivity using python3 is not working
我嘗試了以下基於 coinbase documentaion coinbase doc的代碼該文檔是為 Python2 提供的,但我已經修改並將其用於 Python3,因為我正在嘗試連接到 Coinbase Coinbase Advanced trade doc中的高級交易 API
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 中的高級交易功能。
好的,所以我進行了兩次更新以使其運行。
在request_path = '/api/v3/brokerage/accounts'
中的api
之前插入一個/
:)
將生成timestamp
的方式更改為timestamp = str(int(time.time()))
。
我認為將您的timestamp
更新為字符串會修復它,但事實並非如此,所以我恢復了生成它的方式。 也許有人可以告訴我們為什么一個有效而另一個無效。 如果我弄明白了,我一定會更新這篇文章。
這是完整的工作代碼。 我保持其他一切不變,但用我的替換了你的評論。
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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.