Python3:自定义加密标头URLLIB - KrakenAPI

时间:2017-10-15 15:19:43

标签: python python-3.x api urllib kraken.com

好吧,所以我认为我在这个联盟之外的一点点。 我试图提供自定义HTTP标头,这里注意到:

    API-Key = API key
    API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key

来自https://www.kraken.com/help/api

如果可能的话,我试图完全用urllib工作。

以下是根据需要对其进行编码的众多尝试之一:

    import os
    import sys
    import time
    import datetime
    import urllib.request
    import urllib.parse
    import json
    import hashlib
    import hmac
    import base64

    APIKey = 'ThisKey'
    secret = 'ThisSecret'

    data = {}

    data['nonce'] = int(time.time()*1000)

    data['asset'] = 'ZUSD'

    uripath = '/0/private/TradeBalance'

    postdata = urllib.parse.urlencode(data)

    encoded = (str(data['nonce']) + postdata).encode()
    message = uripath.encode() + hashlib.sha256(encoded).digest()

    signature = hmac.new(base64.b64decode(secret),
                    message, hashlib.sha512)
    sigdigest = base64.b64encode(signature.digest())

    #this is purely for checking how things are coming along.
    print(sigdigest.decode())

    headers = {
    'API-Key': APIKey,
    'API-Sign': sigdigest.decode()
    }

上述情况可能正常,我现在正在努力将其运送到网站上。 这是我最近的尝试:

    myBalance = urllib.urlopen('https://api.kraken.com/0/private/TradeBalance', urllib.parse.urlencode({'asset': 'ZUSD'}).encode("utf-8"), headers)

非常感谢任何帮助。 谢谢!

1 个答案:

答案 0 :(得分:0)

currentUser不支持添加标头,因此您需要创建一个urlopen对象并将其传递给Request

urlopen