0

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())
barciewicz
  • 3,511
  • 6
  • 32
  • 72
  • Did you try to configure `aiohttp` to produce debugging output? What output did you get? It should show the exact HTTP requests that it is sending... – Bakuriu Jan 26 '19 at 23:33
  • 2
    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 – Artemij Rodionov Jan 27 '19 at 03:12
  • Thanks @Artemiy, that did the trick! Please post an answer, so I can accept. – barciewicz Jan 27 '19 at 07:24

1 Answers1

1

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.

Artemij Rodionov
  • 1,721
  • 1
  • 17
  • 22