我试图实现一个使用Authlib和FastAPI框架的Google OAuth示例,我发现的大多数示例都使用Authlib,这是推荐的方法,所以我遵循Authlib文档的FastAPI,但当我遵循这个示例https://blog.authlib.org/2020/fastapi-google-loginfrom Authlib时,我不会改变任何东西,只是试图使用我自己的client_id和client_secret从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
这个例子应该没有错误
多亏了这个github问题,我意识到用str()
包装auth.google.authorize_redirect()
有助于它向前移动:
@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()
我还不能确认流程的其余部分是否可以正常工作,但至少我克服了这个错误。