Ad

How To Fix [Errno 111] Connection Refused

I am setting up an oauth2 server using the authlib (0.11) library. I already created the Oauth2Session and received the authorization code grant. Now I have to fetch_access_token(...), but I am receiving a [Errno 111] Connection refused while trying to communicate with the access_token_url. I am using docker compose, and have linked both with a bridge. I am also using port mapping to access the localhost from the host. (macOS)

I already tried netcat on the localhost and the specific link and sending GET / HTTP/1.1. It worked. I have already checked port forwarding and it is set to true (=1) I tried setting the name of the host link (127.0.0.1) as the docker compose name (backend)

access_token_url = 'http://127.0.0.1:50000/oauth/token'
# tried 'http://backend:50000/oauth/token', 'http://localhost:50000/oauth/token'
token = sessionOauth.fetch_access_token(access_token_url, authorization_response=authorization_response)
# the part that concerns the question
backend:
    build: .

    ports:
      - "50000:5000"
    environment:
      - AUTHLIB_INSECURE_TRANSPORT=true
      - PYTHONUNBUFFERED=1
    networks:
      - db
      - front_back

vuejs:
    build: ../x/
    ports:
      - 8080:8080
    networks:
      - front_back
networks:
    main:
      driver: bridge
    db:
      driver: bridge
    front_back:
      driver: bridge
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1737, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "/var/app/app/routes.py", line 325, in testing3
    token = sessionOauth.fetch_access_token(access_token_url, authorization_response=authorization_response)
  File "/usr/local/lib/python3.7/site-packages/authlib/client/oauth2_session.py", line 102, in fetch_access_token
    return self.fetch_token(url, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/authlib/oauth2/client.py", line 177, in fetch_token
    headers=headers, **session_kwargs
  File "/usr/local/lib/python3.7/site-packages/authlib/oauth2/client.py", line 185, in _fetch_token
    auth=auth, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/authlib/client/oauth2_session.py", line 110, in request
    method, url, auth=auth, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=50000): Max retries exceeded with url: /oauth/token (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc872466198>: Failed to establish a new connection: [Errno 111] Connection refused'))
Ad

Answer

The answer is:

use the name of the container, in my case backend, and then use the original port not the exposed one, in my case 5000.

access_token_url = 'http://backend:5000/oauth/token'
Ad
source: stackoverflow.com
Ad