我实际上正在使用带有 authlib 的 starlette 框架进行 Google OAuth 身份验证的平台上工作。
从今天开始,我500 Internal Server Error
在调用时遇到oauth.google.authorize_redirect(request, redirect_uri)
错误httpx.exceptions.ConnectTimeout
。
我完全迷失了这种新的行为。我可以使用以下demo-oauth-client
提供的存储库重现它authlib
:
https ://github.com/authlib/demo-oauth-client/tree/master/starlette-google-login
app.py 是:
import json
from starlette.config import Config
from starlette.applications import Starlette
from starlette.middleware.sessions import SessionMiddleware
from starlette.responses import HTMLResponse, RedirectResponse
from authlib.integrations.starlette_client import OAuth
app = Starlette(debug=True)
app.add_middleware(SessionMiddleware, secret_key="!secret")
config = Config('.env')
oauth = OAuth(config)
CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration'
oauth.register(
name='google',
server_metadata_url=CONF_URL,
client_kwargs={
'scope': 'openid email profile'
}
)
@app.route('/')
async def homepage(request):
user = request.session.get('user')
if user:
data = json.dumps(user)
html = (
f'<pre>{data}</pre>'
'<a href="/logout">logout</a>'
)
return HTMLResponse(html)
return HTMLResponse('<a href="/login">login</a>')
@app.route('/login')
async def login(request):
redirect_uri = request.url_for('auth')
return await oauth.google.authorize_redirect(request, redirect_uri)
@app.route('/auth')
async def auth(request):
token = await oauth.google.authorize_access_token(request)
user = await oauth.google.parse_id_token(request, token)
request.session['user'] = dict(user)
return RedirectResponse(url='/')
@app.route('/logout')
async def logout(request):
request.session.pop('user', None)
return RedirectResponse(url='/')
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='127.0.0.1', port=8000)
.env
文件是:
GOOGLE_CLIENT_ID=[REDACTED]
GOOGLE_CLIENT_SECRET=[REDCATED]
对这种新行为有任何想法吗?我完全迷失了。谢谢 !!