lepture / authlib

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
https://authlib.org/
BSD 3-Clause "New" or "Revised" License
4.49k stars 448 forks source link

TypeError: cannot convert 'URL' object to bytes with oauth.google.authorize_redirect() #537

Open Jsalaz1989 opened 1 year ago

Jsalaz1989 commented 1 year ago

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.

nurettn commented 1 year ago

535

Jsalaz1989 commented 1 year ago

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()
ezeparziale commented 1 year ago

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()

adding str work fine