首次尝试连接到比特币交易所的私有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文档here。 (测试功能)
答案 0 :(得分:2)
您传递的无效时间戳,在他们提到的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
修复此问题。