我尝试使用aiohttp共享连接cookie时遇到问题,登录函数的响应包含网站设置的cookie,但是当调用add_to_cart函数时,它没有获取cookie,导致非工作程序。
我在互联网上搜索了很多东西,但是没有找到解决我问题的解决方案。
任何帮助将不胜感激,
预先感谢
我得到的结果:
{"success":1,"code":"OK","msg":"OK","data":{"session_name":"Login-Token","session_id":"the _session_id","nickname":"my_nickname","displayname":"my_displayname"}}
{"success":0,"code":"ErrNotAuthenticated","msg":"You must be authenticated to reach this area.","data":null}
{"success":0,"code":"ErrNotAuthenticated","msg":"You must be authenticated to reach this area.","data":null}
Python代码:
async def login(session):
payload = {"username": logins[0][0], "password": logins[0][1], "remember": "on"}
async with session.post("https://example.com/api/account/signin", json=payload) as resp:
print(await resp.text())
async def add_to_cart(session):
payload = {"skus":{"12680":1}}
async with session.post("https://example.com/api/store/addToCart", json=payload) as resp:
print(await resp.text())
async def validate_cart(session):
async with session.post("https://example.com/api/store/validateCart") as resp:
print(await resp.text())
async def manage_cart(session):
await login(session)
await add_to_cart(session)
await validate_cart(session)
async def main():
jar = aiohttp.CookieJar(unsafe=True)
async with aiohttp.ClientSession(cookie_jar=jar) as session:
await manage_cart(session)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_until_complete(asyncio.sleep(0.250))
loop.close()