authlib icon indicating copy to clipboard operation
authlib copied to clipboard

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

Open Jsalaz1989 opened this issue 2 years ago • 3 comments

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.

Jsalaz1989 avatar Apr 02 '23 17:04 Jsalaz1989

#535

nurettn avatar Apr 02 '23 19:04 nurettn

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

Jsalaz1989 avatar Apr 03 '23 10:04 Jsalaz1989

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

ezeparziale avatar May 10 '23 21:05 ezeparziale