无法使用异步函数序列化非 str 键错误,而该函数在同步时没有问题

问题描述 投票:0回答:2

我有一个可以正常工作的api请求功能。我的签名序列化得很好,API 请求也会通过,但是当我将其转换为异步函数时,它给我带来了一个真正不受函数异步性影响的序列化问题。

我得到:#Signature-无法序列化非 str 密钥 b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ=

async def kucoin_order(pair,side,amount):#Signature-Cannot serialize non-str key b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ='
    kucoin_balance,api_key, api_secret, api_passphrase = {},settings_feed['kucoin_key'], settings_feed['kucoin_secret'], settings_feed['kucoin_quote']
    timestamp = str(int(round(time.time() * 1000)))
    data = {"clientOid": timestamp,"side": side,"symbol": pair,"type": "market","size": amount}
    data_json = json.dumps(data)
    str_to_sign = str(timestamp) + 'POST' + '/api/v1/orders' + data_json
    signature=base64.b64encode(hmac.new(api_secret.encode('utf-8'),str_to_sign.encode('utf-8'),hashlib.sha256).digest())
    passphrase=base64.b64encode(hmac.new(api_secret.encode('utf-8'),api_passphrase.encode('utf-8'),hashlib.sha256).digest())
    headers = {"KC-API-SIGN": signature,#Cannot serialize non-str key b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ='
               "KC-API-TIMESTAMP": str(timestamp),
               "KC-API-KEY": api_key,
               "KC-API-PASSPHRASE":passphrase,
               "KC-API-KEY-VERSION":"2",
               "Content-Type":"application/json"}
    try:
        async with aiohttp.ClientSession() as session:
            assets = await session.post(f"https://api.kucoin.com/api/v1/orders",headers = headers, data = data_json)
            if not assets.status == 200:raise Exception('Status code not 200')
            assets = await assets.json()
            if 'status' in assets and assets['status'] == 'error': raise Exception('Status code not 200')
            if debug: print('Kucoin order for', pair, assets)
        response = requests.post(url, headers=headers, data=data_json).json()
        print(response.json())
    except Exception as err:
        print(err)

我已经尝试了签名字节码编码的不同变体,但没有成功。因此,即使您没有 api 密钥/通行证,您仍然可以看到它无法正确编码。没有返回错误,只是它无法正确编码。

python api serialization bytecode kucoin
2个回答
2
投票

aiohttp
不会自动将标头、参数等字典中的布尔值和字节转换为字符串。自己做吧。

async def kucoin_order(pair,side,amount):
    kucoin_balance,api_key, api_secret, api_passphrase = {},settings_feed['kucoin_key'], settings_feed['kucoin_secret'], settings_feed['kucoin_quote']
    timestamp = str(int(round(time.time() * 1000)))
    data = {"clientOid": timestamp,"side": side,"symbol": pair,"type": "market","size": amount}
    data_json = json.dumps(data)
    str_to_sign = str(timestamp) + 'POST' + '/api/v1/orders' + data_json
    signature=base64.b64encode(hmac.new(api_secret.encode('utf-8'),str_to_sign.encode('utf-8'),hashlib.sha256).digest())
    passphrase=base64.b64encode(hmac.new(api_secret.encode('utf-8'),api_passphrase.encode('utf-8'),hashlib.sha256).digest())
    headers = {"KC-API-SIGN": signature.decode('utf-8'),  # Add .decode('utf-8')
               "KC-API-TIMESTAMP": str(timestamp),
               "KC-API-KEY": api_key,
               "KC-API-PASSPHRASE":passphrase,
               "KC-API-KEY-VERSION":"2",
               "Content-Type":"application/json"}
    try:
        async with aiohttp.ClientSession() as session:
            assets = await session.post(f"https://api.kucoin.com/api/v1/orders",headers = headers, data = data_json)
            if not assets.status == 200:raise Exception('Status code not 200')
            assets = await assets.json()
            if 'status' in assets and assets['status'] == 'error': raise Exception('Status code not 200')
            if debug: print('Kucoin order for', pair, assets)
        response = requests.post(url, headers=headers, data=data_json).json()
        print(response.json())
    except Exception as err:
        print(err)

0
投票

您可以将签名解码为 utf-8 并且不使用同步请求库

signature = base64.b64encode(
        hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest()).decode('utf-8')

passphrase = base64.b64encode(
        hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest()).decode('utf-8')
headers = {
        "KC-API-SIGN": signature,
        "KC-API-TIMESTAMP": str(now),
        "KC-API-KEY": api_key,
        "KC-API-PASSPHRASE": passphrase,
        "KC-API-KEY-VERSION": "2"
    }
async with ClientSession() as session:
    response = await session.get(url, headers=headers)
    data = await response.json()

这对我来说是工作

© www.soinside.com 2019 - 2024. All rights reserved.