0

I'm trying to implement an example of Google OAuth using Authlib and FastAPI framework, most of the examples I found are using Authlib and that's the recommended approach, so I'm following the Authlib documentation for FastAPI but getting this error when I follow this example https://blog.authlib.org/2020/fastapi-google-loginfrom Authlib, I'm not changing anything just trying to run the project using my own client_id and client_secret from Google.

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 436, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/fastapi/applications.py", line 276, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/middleware/sessions.py", line 86, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await dependant.call(**values)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/main.py", line 41, in login
    return await oauth.google.authorize_redirect(request, redirect_uri)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/integrations/starlette_client/apps.py", line 29, in authorize_redirect
    rv = await self.create_authorization_url(redirect_uri, **kwargs)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/integrations/base_client/async_app.py", line 105, in create_authorization_url
    return self._create_oauth2_authorization_url(
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/integrations/base_client/sync_app.py", line 265, in _create_oauth2_authorization_url
    url, state = client.create_authorization_url(
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/oauth2/client.py", line 155, in create_authorization_url
    uri = prepare_grant_uri(
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/oauth2/rfc6749/parameters.py", line 66, in prepare_grant_uri
    return add_params_to_uri(uri, params)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/common/urls.py", line 99, in add_params_to_uri
    query = add_params_to_qs(query, params)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/common/urls.py", line 90, in add_params_to_qs
    return url_encode(qs)
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/venv/lib/python3.10/site-packages/authlib/common/urls.py", line 28, in url_encode
    encoded.append((to_bytes(k), to_bytes(v)))
  File "/Users/alvartabe/Desktop/FastAPI-GoogleOAuth/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

The example should work without errors

1 Answers1

3

Thanks to this github issue I realized that wrapping auth.google.authorize_redirect() with str() helps it move along:

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

I can't confirm yet if the rest of the flow will work fine, but at least I got past that error.

Jaime Salazar
  • 349
  • 1
  • 2
  • 11