Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不确定如何验证我的api密钥_Python_Parameters_Request - Fatal编程技术网

Python 不确定如何验证我的api密钥

Python 不确定如何验证我的api密钥,python,parameters,request,Python,Parameters,Request,我正在学习如何使用Kucoin,在向API服务器验证自己时遇到了麻烦 我正在尝试加载所有的活动订单,但始终出现401错误 Kucoin API文档说明我需要添加以下内容: { "KC-API-KEY": "59c5ecfe18497f5394ded813", "KC-API-NONCE" : 1506219855000 //Client timestamp (exact to milliseconds), before using the calibration tim

我正在学习如何使用Kucoin,在向API服务器验证自己时遇到了麻烦

我正在尝试加载所有的活动订单,但始终出现401错误

Kucoin API文档说明我需要添加以下内容:

{
    "KC-API-KEY": "59c5ecfe18497f5394ded813",  
    "KC-API-NONCE" : 1506219855000   //Client timestamp (exact to 
milliseconds), before using the calibration time, the server does not 
accept calls with a time difference of more than 3 seconds
    "KC-API-SIGNATURE" : 
"fd83147802c361575bbe72fef32ba90dcb364d388d05cb909c1a6e832f6ca3ac"   
//signature after client encryption
}

作为请求头的参数。我不确定这意味着什么。任何帮助都将不胜感激。

创建标题可能有点棘手

对于nonce值或任何毫秒时间戳值,我发现生成它的最佳方法如下

import time
int(time.time() * 1000)
签名要求您以查询字符串格式按字母顺序排列参数,将其与路径和nonce组合,然后使用sha256和密钥对字符串进行散列

如果您想自己实现它,请随意从这里复制代码,它分为几个函数,应该非常可读


或者,您最好使用该库。(注意:我是python kucoin的作者和维护者)

以下是我在python 3中的工作代码:

import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode
import time

api_key = 'xxxxx'
api_secret = 'xx-xxx-xx'
api_passphrase = 'xxx'   #this is NOT trading password
base_uri = 'https://api.kucoin.com'

def get_headers(method, endpoint):
    now = int(time.time() * 1000)
    str_to_sign = str(now) + method + endpoint
    signature = base64.b64encode(hmac.new(api_secret.encode(), str_to_sign.encode(), hashlib.sha256).digest()).decode()
    passphrase = base64.b64encode(hmac.new(api_secret.encode(), api_passphrase.encode(), hashlib.sha256).digest()).decode()
    return {'KC-API-KEY': api_key,
            'KC-API-KEY-VERSION': '2',
            'KC-API-PASSPHRASE': passphrase,
            'KC-API-SIGN': signature,
            'KC-API-TIMESTAMP': str(now)
    }

#List Accounts
method = 'GET'
endpoint = '/api/v1/accounts'
response = requests.request(method, base_uri+endpoint, headers=get_headers(method,endpoint))
print(response.status_code)
print(response.json())
输出

200
{'code': '200000', 'data': [{'available': blah, blah blah  }]}

你明白了吗?我在验证自己时遇到了问题too@Bas我还在想办法,如果你能想出来,我会在下周继续!谢谢,有很多事要做,但我会努力完成的。