-
Environment:
I am trying to switch from request session to aiohttp session to do asynchronous requests. My code looks like the following: class Client():
API_URL = my_url
def __init__(self, api_key, api_secret):
self.API_KEY = api_key
self.API_SECRET = api_secret
self.session = self._init_async_session()
def _init_async_session(self):
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession(
loop=loop,
headers={
'Accept': 'application/json',
'X-MBX-APIKEY': self.API_KEY
}
)
return session
def _hashing(self, query_string):
return hmac.new(SECRET.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
async def send_async_request(self, **params):
query_string = urlencode(params)
query_string = query_string.replace('%27', '%22')
query_string = "{}×tamp={}".format(query_string, int(time.time() * 1000))
url = self.API_URL + '/batchOrders?' + query_string + '&signature=' + self._hashing(query_string)
params = {'url': url, 'params': {}}
async with getattr(self.session, 'post')(**params) as response:
return await response.json()
async def close(self):
await self.session.close() But when calling the aiohttp session like that: async def startFuture():
client = Client(KEY, SECRET)
response = await client.send_async_request(batchOrders=json.dumps(batchOrders))
await asyncio.sleep(1)
print(response)
await client.session.close()
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(startFuture()) I get the error: > {'code': -1022, 'msg': 'Signature for this request is not valid.'} This invalid signature is the response from the serveur. However, it looks like the session from aiohttp does not behave the same as the session from requests for this code when using session from requests works perfectly... Do you have any idea why is that please? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
aiohttp 3.4 is not supported anymore, you should migrate to 3.6+. Also, to prove that it's a bug you must provide a reproducer consisting solely of aiohttp-related code and being as small as possible. There's no evidence that this message is even coming from aiohttp: there's no such string in the source — https://github.com/aio-libs/aiohttp/search?q=Signature+for+this+request+is+not+valid. I'm converting this to a discussion because it's clearly a support request and maybe somebody from the community will be able to help you out. But you should at least show the full output of the programs you run and the way you invoke them, otherwise, nobody will be able to guess what it is you're attempting to do. |
Beta Was this translation helpful? Give feedback.
aiohttp 3.4 is not supported anymore, you should migrate to 3.6+. Also, to prove that it's a bug you must provide a reproducer consisting solely of aiohttp-related code and being as small as possible. There's no evidence that this message is even coming from aiohttp: there's no such string in the source — https://github.com/aio-libs/aiohttp/search?q=Signature+for+this+request+is+not+valid.
I'm converting this to a discussion because it's clearly a support request and maybe somebody from the community will be able to help you out. But you should at least show the full output of the programs you run and the way you invoke them, otherwise, nobody will be able to guess what it is you're attem…