Socket error occuring on WSL2 – socket.gaierror: [Errno -3] Temporary failure in name resolution

I’m running a docker container on Windows Subsystem for Linux 2. When I attempt to log in with google Oauth it give me this error when I attempt to fetch the token from google’s API:

prosebit_1  | [2019-11-07 14:05:05 +0000] [8] [ERROR] Socket error 
processing request.
prosebit_1  | Traceback (most recent call last):
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 157, in _new_conn
prosebit_1  |     (self._dns_host, self.port), self.timeout, **extra_kw
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 61, in create_connection
prosebit_1  |     for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
prosebit_1  |   File "/usr/local/lib/python3.7/socket.py", line 748, in getaddrinfo
prosebit_1  |     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
prosebit_1  | socket.gaierror: [Errno -3] Temporary failure in name resolution
prosebit_1  |
prosebit_1  | During handling of the above exception, another exception occurred:
prosebit_1  |
prosebit_1  | Traceback (most recent call last):
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 672, in urlopen
prosebit_1  |     chunked=chunked,
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 376, in _make_request
prosebit_1  |     self._validate_conn(conn)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
prosebit_1  |     conn.connect()
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 334, in connect
prosebit_1  |     conn = self._new_conn()
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 169, in _new_conn
prosebit_1  |     self, "Failed to establish a new connection: %s" % e
prosebit_1  | urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f6b838c8050>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution
prosebit_1  |
prosebit_1  | During handling of the above exception, another exception occurred:
prosebit_1  |
prosebit_1  | Traceback (most recent call last):
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
prosebit_1  |     timeout=timeout
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 720, in urlopen
prosebit_1  |     method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 436, in increment
prosebit_1  |     raise MaxRetryError(_pool, url, error or ResponseError(cause))
prosebit_1  | urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6b838c8050>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
prosebit_1  |
prosebit_1  | During handling of the above exception, another exception occurred:
prosebit_1  |
prosebit_1  | Traceback (most recent call last):
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 135, in handle
prosebit_1  |     self.handle_request(listener, req, client, addr)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 191, in handle_request
prosebit_1  |     six.reraise(*sys.exc_info())
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/six.py", line 625, in reraise
prosebit_1  |     raise value
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
prosebit_1  |     respiter = self.wsgi(environ, resp.start_response)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
prosebit_1  |     return self.wsgi_app(environ, start_response)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/werkzeug/middleware/proxy_fix.py", line 232, in __call__
prosebit_1  |     return self.app(environ, start_response)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
prosebit_1  |     response = self.handle_exception(e)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
prosebit_1  |     reraise(exc_type, exc_value, tb)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
prosebit_1  |     raise value
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
prosebit_1  |     response = self.full_dispatch_request()
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
prosebit_1  |     rv = self.handle_user_exception(e)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
prosebit_1  |     reraise(exc_type, exc_value, tb)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
prosebit_1  |     raise value
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
prosebit_1  |     rv = self.dispatch_request()
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
prosebit_1  |     return view_func(**req.view_args)
prosebit_1  |   File "/prosebit/prosebit/blueprints/page/views.py", line 212, in oauth2callback
prosebit_1  |     token = google.fetch_token( current_app.config['TOKEN_URI'], client_secret=current_app.config['CLIENT_SECRET'], authorization_response=request.url)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests_oauthlib/oauth2_session.py", line 344, in fetch_token
prosebit_1  |     **request_kwargs
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests_oauthlib/oauth2_session.py", line 516, in request
prosebit_1  |     method, url, headers=headers, data=data, **kwargs
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
prosebit_1  |     resp = self.send(prep, **send_kwargs)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
prosebit_1  |     r = adapter.send(request, **kwargs)
prosebit_1  |   File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
prosebit_1  |     raise ConnectionError(e, request=request)
prosebit_1  | requests.exceptions.ConnectionError: HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6b838c8050>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

Interestingly, I’ve tried two separate methods which both perfectly when I run it on docker for windows, but not on WSL2 which makes me think it has something to do with the ports being used by WSL. Is These are two lines that fail to connect for each respective method I’ve attempted:

Method 1 using flow object:

access = flow.fetch_token(authorization_response=authorization_response)

Method 2 using OAuth2Session from requests_oauthlib:

token = google.fetch_token( current_app.config['TOKEN_URI'], client_secret=current_app.config['CLIENT_SECRET'], authorization_response=request.url)

Source: StackOverflow