responder icon indicating copy to clipboard operation
responder copied to clipboard

Exception in ASGI application when using requests post

Open sladkovm opened this issue 5 years ago • 1 comments

I have implemented oauth2 flow with Strava API some time ago and it is up and running at http://velometria.com/strava-oauth/authorize

If I deploy this app now however, I get this error:

strava-oauth    | DEBUG: Requesting: https://www.strava.com/oauth/authorize?client_id=XXXX&response_type=code&redirect_uri=http%3A%2F%2Fdev.velometria.com%2Fauthorization_successful&scope=read%2Cprofile%3Aread_all%2Cactivity%3Aread&state=https%3A%2F%2Fgithub.com%2Fsladkovm%2Fstrava-oauth&approval_prompt=force
strava-oauth    | INFO: ('192.168.99.1', 52408) - "GET /authorize HTTP/1.1" 301
strava-oauth    | DEBUG: Exchaning code for token with {'client_id': 'XXXX', 'client_secret': 'XXXX', 'code': 'XXXX', 'grant_type': 'authorization_code'}
strava-oauth    | INFO: ('192.168.99.1', 52430) - "GET /authorization_successful HTTP/1.1" 500
strava-oauth    | ERROR: Exception in ASGI application
strava-oauth    | Traceback (most recent call last):
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
strava-oauth    |     result = await app(self.scope, self.receive, self.send)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/uvicorn/middleware/asgi2.py", line 7, in __call__
strava-oauth    |     await instance(receive, send)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/starlette/middleware/errors.py", line 125, in asgi
strava-oauth    |     raise exc from None
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/starlette/middleware/errors.py", line 103, in asgi
strava-oauth    |     await asgi(receive, _send)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/starlette/middleware/gzip.py", line 33, in __call__
strava-oauth    |     await self.inner(receive, self.send_with_gzip)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/responder/api.py", line 264, in asgi
strava-oauth    |     req, scope=scope, send=send, receive=receive
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/responder/api.py", line 353, in _dispatch_request
strava-oauth    |     await self._execute_route(route=route, req=req, resp=resp, **options)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/responder/api.py", line 376, in _execute_route
strava-oauth    |     await r
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/responder/background.py", line 44, in __call__
strava-oauth    |     return await run_in_threadpool(func, *args, **kwargs)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/starlette/concurrency.py", line 24, in run_in_threadpool
strava-oauth    |     return await loop.run_in_executor(None, func, *args)
strava-oauth    |   File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
strava-oauth    |     result = self.fn(*self.args, **self.kwargs)
strava-oauth    |   File "api.py", line 59, in authorization_successful
strava-oauth    |     r = requests.post("https://www.strava.com/oauth/token", params)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 116, in post
strava-oauth    |     return request('post', url, data=data, json=json, **kwargs)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 60, in request
strava-oauth    |     return session.request(method=method, url=url, **kwargs)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
strava-oauth    |     resp = self.send(prep, **send_kwargs)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
strava-oauth    |     r = adapter.send(request, **kwargs)
strava-oauth    |   File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
strava-oauth    |     raise ConnectionError(e, request=request)
strava-oauth    | requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.strava.com', port=443): Max retries exceeded with url: /oauth/token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7ff501667a58>: Failed to establish a new connection: [Errno -2] Name or service not known',))

The authorization_succesfull route is implemented like this:

@api.route("/authorization_successful")
def authorization_successful(req, resp):
    """Exchange code for a user token"""
    params = {
        "client_id": os.getenv('STRAVA_CLIENT_ID'),
        "client_secret": os.getenv('STRAVA_CLIENT_SECRET'),
        "code": req.params.get('code'),
        "grant_type": "authorization_code"
    }
    logger.debug(f"Exchaning code for token with {params}")
    r = requests.post("https://www.strava.com/oauth/token", params)
    resp.text = r.text

The most surprising that the app deployed few month ago is still app and running. Some issues with new versions of requests, responder?

sladkovm avatar Apr 26 '19 06:04 sladkovm

Update:

  1. If I don't execute the requests.post(), but rather return some resp.text - the route executes well.
  2. Rolling back to requests==2.21.0 does not help

Something with https?

sladkovm avatar Apr 26 '19 11:04 sladkovm