我正在尝试获取(加密货币交易所的) API,以便更新我的电话号码:
bitso_key = 'API_KEY'
bitso_secret ='API_SECRET'
consulta="phone_number"
phone_number=5534970199
nonce = str(int(round(time.time() * 1000)))
http_method = "POST"
request_path = "/v3/"+consulta+"?"
json_payload={"phone_number":phone_number}
# Create signature
message = nonce+http_method+request_path+urlencode(json_payload)
#print(message)
signature = hmac.new(bitso_secret.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256).hexdigest()
# Build the auth header
auth_header = 'Bitso %s:%s:%s' % (bitso_key, nonce, signature)
url="https://api.bitso.com"+request_path
response = requests.post(url, data=json_payload, headers={"Authorization":
auth_header}).json()
我不明白为什么,但响应总是身份验证错误。当我对GET请求执行相同的代码时,它可以工作:
consulta="user_trades"
book="eth_mxn"
limit="2"
nonce = str(int(round(time.time() * 1000)))
http_method = "GET"
request_path = "/v3/"+consulta+"?"
json_payload={"book":book,"limit":limit}
# Create signature
message = nonce+http_method+request_path+urlencode(json_payload)
signature = hmac.new(bitso_secret.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256).hexdigest()
print(signature)
# Build the auth header
auth_header = 'Bitso %s:%s:%s' % (bitso_key, nonce, signature)
url="https://api.bitso.com"+request_path
print(url,message)
# Send request
response = requests.get(url, params=json_payload, headers={"Authorization":
auth_header}).json()
我认为这是变量"message“的问题,它是用来为请求创建签名的,我不知道如何使用json_payload正确创建它。
edit:用python 3编写,请求POST的未成功响应为:
{'error': {'code': '0201', 'message': 'Invalid Nonce or Invalid
Credentials'}, 'success': False}
发布于 2018-06-20 10:37:06
使用以下代码:
import time
import hmac
import hashlib
import requests
import json
import random
import sys
def main(argv):
# Bitso API url
bitso_url = "https://api.bitso.com"
# Obtained api and secret keys for handling private requests
bitso_key = "<KEY>"
bitso_secret = "<SECRET>"
# A valid number to avoid replay attacks
nonce = str(int(round(time.time())) * 100000 * 2)
# Desired HTTP method
http_method = "POST"
request_path = "/api/v3/<POST_ENDPOINT>"
# The required info that endpoint requires to process info
payload = {}
#Add required keys of the parameters to the json
#As follows
#payload["<needed key>"] = <key value>
#json encoding
json_payload = json.dumps(payload)
# Create secure signature with BITSO API documentation specification
message = nonce + http_method + request_path + json_payload
signature = hmac.new(bitso_secret.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256).hexdigest()
# Build the auth header
auth_header = 'Bitso %s:%s:%s' % (bitso_key, nonce, signature)
# Send request
response = requests.post(bitso_url+request_path,
headers = {"Authorization" : auth_header},
json = payload)
print(response.content)
if __name__ == "__main__":
main(sys.argv[1:])
https://stackoverflow.com/questions/47384049
复制相似问题