首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用API提供的加密签名为python请求POST

使用API提供的加密签名为python请求POST
EN

Stack Overflow用户
提问于 2017-11-20 09:54:30
回答 1查看 2.4K关注 0票数 1

我正在尝试获取(加密货币交易所的) API,以便更新我的电话号码:

代码语言:javascript
运行
复制
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请求执行相同的代码时,它可以工作:

代码语言:javascript
运行
复制
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的未成功响应为:

代码语言:javascript
运行
复制
{'error': {'code': '0201', 'message': 'Invalid Nonce or Invalid 
Credentials'}, 'success': False} 
EN

回答 1

Stack Overflow用户

发布于 2018-06-20 10:37:06

使用以下代码:

代码语言:javascript
运行
复制
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:])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47384049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档