我正在使用authlib(0.11)库设置oauth2服务器。我已经创建了Oauth2Session并收到了授权码授予。现在我必须要fetch_access_token(...),但是我在尝试与access_token_url通讯时收到[Errno 111]连接被拒绝。我正在使用docker compose,并且已将两者与桥链接在一起。我还使用端口映射从主机访问本地主机。 (macOS)
我已经在本地主机和特定链接上尝试过netcat并发送GET / HTTP / 1.1。有效。 我已经检查过端口转发并将其设置为true(= 1) 我尝试将主机链接的名称(127.0.0.1)设置为docker compose名称(后端)
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'))
答案 0 :(得分:0)
答案是:
使用容器的名称(在我的情况下为后端),然后使用原始端口而不是暴露的端口(在我的情况下为5000)。
access_token_url = 'http://backend:5000/oauth/token'