python 无法序列化异步中的函数的非str键错误,该函数在同步时没有问题

2ul0zpep  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(606)

我有一个工作正常的API请求函数,我的签名序列化得很好,api请求也会通过,但是当我把这个转换成一个异步函数的时候,它给了我一个序列化的问题,这个问题确实不受函数异步性的影响。
我得到:#签名-无法序列化非字符串键b 'aQbzHhquttFv + Kpd 9 TNkTHOmNy 183 ybJZTEP 8 PoDJyQ =

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密钥/密码,你仍然可以看到它不能正确地编码,没有返回错误,只是它不能正确地编码。

vh0rcniy

vh0rcniy1#

aiohttp不会自动将bool和bytes转换为头文件、参数等字典中的字符串。请自行转换。

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)

相关问题