barciewicz
barciewicz

Reputation: 3793

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

I am using the below code to make 599 asynchronous requests to Strava API. For some reason the response I get for each of them is

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

This is the type of error you typically get when your access_token query string parameter is invalid. But in this case the token is 100% correct: the URL returns correct response when just copy-pasted manually in the browser.

What might be the reason of the error and how to fix it? Might it be that the aiohttp session is somehow messing the authentication procedure up?

Note: for privacy reasons the token in the code below is fake.

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())

Upvotes: 0

Views: 566

Answers (1)

Artemij Rodionov
Artemij Rodionov

Reputation: 1826

You shouldn't use a multiline string as the URL, because it will keep all whitespaces and as a result you will get the wrong URL.

Upvotes: 1

Related Questions