authlib
authlib copied to clipboard
TypeError: cannot convert 'URL' object to bytes with oauth.google.authorize_redirect()
Same issue as this SO user. I'm following the official tutorial / blog post, but I get the following error when it gets to oauth.google.authorize_redirect():
Code from tutorial:
@app.get("/login/google")
async def login_via_google(request: Request):
redirect_uri = request.url_for('auth_via_google')
return await oauth.google.authorize_redirect(request, redirect_uri) # breaks here
@app.get("/auth/google")
async def auth_via_google(request: Request):
token = await oauth.google.authorize_access_token(request)
user = token['userinfo']
return dict(user)
The error:
File "/home/.venv/lib/python3.10/site-packages/authlib/common/encoding.py", line 15, in to_bytes
return bytes(x)
TypeError: cannot convert 'URL' object to bytes
I'm on fastapi==0.95.0 and Authlib==1.2.0.
#535
Thanks for linking to that, it helped me realize that, at least in my case, wrapping the oauth.google.authorize_redirect() with str does the trick:
@app.get("/login/google")
async def login_via_google(request: Request):
redirect_uri = request.url_for('auth_via_google')
return await oauth.google.authorize_redirect(request, str(redirect_uri)) # wrap with str()
Thanks for linking to that, it helped me realize that, at least in my case, wrapping the
oauth.google.authorize_redirect()withstrdoes the trick:@app.get("/login/google") async def login_via_google(request: Request): redirect_uri = request.url_for('auth_via_google') return await oauth.google.authorize_redirect(request, str(redirect_uri)) # wrap with str()
adding str work fine