[英]How to Create API Signature with application/x-www-form-urlencoded
第一次嘗試連接到比特幣交易所的私有API,我已經陷入嘗試用我的代碼進行測試調用的過程。
from urllib2 import Request, urlopen
from urllib import urlencode
import datetime
api_key = "myAPIkey"
api_secret = "mySercetKey"
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
values = urlencode({"timestamp" : timestamp})
headers = {"Content-Type": "application/x-www-form-urlencoded", "key": api_key, "sig": api_secret}
request = Request("https://www.fybsg.com/api/SGD/test", data=values, headers=headers)
response_body = urlopen(request).read()
print response_body
這是response_body返回的內容:
{"error":"An unexpected error occurred, its probably your fault, go read the docs."}
善良的靈魂可以指出我的代碼有什么問題嗎? (我有一種嚴重的錯誤感覺),可以在此處找到有關比特幣交換的API文檔。 (測試功能)
您傳遞的時間戳無效,在API文檔中他們提到timestamp
必須是Current Unix timestamp
,可以通過以下方式實現:
timestamp = datetime.datetime.now()
timestamp = int(time.mktime(timestamp.timetuple()))
要不就:
import time
timestamp= int(time.time())
所以更新代碼后
from urllib2 import Request, urlopen
from urllib import urlencode
import datetime
import time
api_key = "myAPIkey"
api_secret = "mySercetKey"
timestamp = datetime.datetime.now() #.strftime('%Y-%m-%d %H:%M:%S')
timestamp = int(time.mktime(timestamp.timetuple()))
print timestamp
values = urlencode({"timestamp" : timestamp})
#sig - HMAC-SHA1 signature of POST Data with Key's Secret
from hashlib import sha1
import hmac
hashed = hmac.new(values, api_secret, sha1)
hashed_value = hashed.digest().encode("base64").rstrip('\n')
headers = {"Content-Type": "application/x-www-form-urlencoded",
"key": api_key, "sig":hashed_value}
request = Request("https://www.fybsg.com/api/SGD/test", data=values, headers=headers)
response_body = urlopen(request).read()
print response_body
我得到這個回應:
{"error":"Invalid API Key or account number"}
我認為您可以使用有效的private key
或account number
來解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.