aiohttp: 多次请求相同 URL return 身份验证错误,但 URL 是正确的

aiohttp: multiple requests to same URL return authentication error, but the URL is correct

我正在使用以下代码向 Strava 发出 599 个异步请求 API。 出于某种原因,我对他们每个人的回应是

{"message":"Authorization Error","errors":[{"resource":"Application","field":"","code":"invalid"}]}

这是您在 access_token 查询字符串参数时通常遇到的错误类型 是无效的。 但在这种情况下,令牌是 100% 正确的: URL returns 正确的响应只是 在浏览器中手动复制粘贴。

错误的可能原因是什么以及如何解决?可能是 aiohttp 会话以某种方式 搞乱身份验证程序?

注意:出于隐私原因,以下代码中的令牌是假的。

import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url) as response:
        print(await response.text())

async def main():
    urls = ['''https://www.strava.com/api/v3/activities/
            280816027?include_all_efforts=true&
            access_token=11111111'''] * 599
    async with aiohttp.ClientSession() as session:
        tasks = [
                    asyncio.ensure_future(fetch(session, url))
                    for url in urls
        ]
        await asyncio.gather(*tasks)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

你不应该使用多行字符串作为 URL,因为它会保留所有空格,结果你会得到错误的 URL。