TypeError: Invalid "auth" argument #1697
-
I was writing a google oauth2 login feature for my starlette/fastapi application. But on going to TypeError: Invalid "auth" argument: <httpx._config.UnsetType object at 0x7fbd7bcbc8e0> I googled and asked on the python-discord server too, got no working solutions sadly. I am not sure if this is the right place, if so please point me to the right place. Here is my code:import typing
import databases
import sqlalchemy
from authlib.integrations.starlette_client import OAuth
from starlette.applications import Starlette
from starlette.middleware import Middleware, sessions
from starlette.responses import JSONResponse, RedirectResponse
from starlette.routing import Mount, Route
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from app.constants import *
templates = Jinja2Templates(directory="app/templates")
oauth = OAuth(config)
oauth.register(
name='google',
server_metadata_url=CONF_URL,
client_kwargs={
'scope': 'openid email profile'
}
)
# Database table definitions.
metadata = sqlalchemy.MetaData()
CANDIDATES_TABLE = sqlalchemy.Table(
"candidates",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("about", sqlalchemy.String),
sqlalchemy.Column("title", sqlalchemy.Boolean),
)
database = databases.Database(DATABASE_URL)
# area routes: start
async def homepage(request):
user = request.session.get('user')
profile_link = None
name = None
is_admin = False
if user:
profile_link = user.get('picture')
name = user.get('name')
is_admin = user.get('email') in ADMIN_EMAIL_IDS
return templates.TemplateResponse(
"index.html",
{
"page_title": "Home",
"is_admin": is_admin,
"profile_link": profile_link,
"name": name,
"request": request
},
)
async def login(request):
"""
Redirect the user to google authorization, the flow continues in /auth.
Unlike other endpoints, you should open this one in the browser, since it redirects to a google website.
"""
redirect_uri = request.url_for('auth')
return await oauth.google.authorize_redirect(request, redirect_uri)
async def auth(request):
token = await oauth.google.authorize_access_token(request)
user = await oauth.google.parse_id_token(request, token)
request.session['user'] = dict(user)
return RedirectResponse(url='/')
async def logout(request):
request.session.pop('user', None)
return RedirectResponse(url='/')
async def list_candidates(request):
"""Get list of all nominees."""
user = request.session.get('user')
if not user:
return RedirectResponse(url='/login?next=candidates')
profile_link = user.get('picture')
name = user.get('name')
is_admin = user.get('email') in ADMIN_EMAIL_IDS
query = CANDIDATES_TABLE.select()
results = await database.fetch_all(query)
candidates = [
{
"about": result["about"],
"title": result["title"]
}
for result in results
]
return templates.TemplateResponse(
"list_candidates.html",
{
"page_title": "Candidates",
"candidates": candidates,
"is_admin": is_admin,
"profile_link": profile_link,
"name": name,
"request": request
},
)
async def add_note(request):
data = await request.json()
query = CANDIDATES_TABLE.insert().values(
about=data["about"],
title=data["title"]
)
await database.execute(query)
return JSONResponse({
"about": data["about"],
"title": data["title"]
})
# area: routes end
middleware = [
Middleware(sessions.SessionMiddleware, secret_key="!secret")
]
routes = [
Route("/", endpoint=homepage),
Route("/login", endpoint=login),
Route("/logout", endpoint=logout),
Route("/auth", endpoint=auth),
Route("/candidates", endpoint=list_candidates),
Route("/candidates/new", endpoint=add_note, methods=["POST"]),
Mount('/static', app=StaticFiles(directory='app/static'), name="static"),
]
app = Starlette(
routes=routes,
middleware=middleware,
on_startup=[database.connect],
on_shutdown=[database.disconnect]
) Error with the traceback: Save New Duplicate & Edit Just Text Twitter
Traceback (most recent call last):
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__
return await self.app(scope, receive, send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/middleware/sessions.py", line 75, in __call__
await self.app(scope, receive, send_wrapper)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
await route.handle(scope, receive, send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
await self.app(scope, receive, send)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
response = await func(request)
File "/home/shivansh/PythonProjects/voting-application-fastapi/app/main.py", line 66, in login
return await oauth.google.authorize_redirect(request, redirect_uri)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/authlib/integrations/starlette_client/integration.py", line 52, in authorize_redirect
rv = await self.create_authorization_url(redirect_uri, **kwargs)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/authlib/integrations/base_client/async_app.py", line 51, in create_authorization_url
metadata = await self.load_server_metadata()
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/authlib/integrations/base_client/async_app.py", line 20, in load_server_metadata
metadata = await self._fetch_server_metadata(self._server_metadata_url)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/authlib/integrations/base_client/async_app.py", line 204, in _fetch_server_metadata
resp = await client.request('GET', url, withhold_token=True)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 94, in request
return await super(AsyncOAuth2Client, self).request(
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/httpx/_client.py", line 1481, in request
response = await self.send(
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/httpx/_client.py", line 1566, in send
auth = self._build_request_auth(request, auth)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/httpx/_client.py", line 433, in _build_request_auth
self._auth if isinstance(auth, UseClientDefault) else self._build_auth(auth)
File "/home/shivansh/.cache/pypoetry/virtualenvs/voting-application-fastapi-nxHNqp5f-py3.9/lib/python3.9/site-packages/httpx/_client.py", line 425, in _build_auth
raise TypeError(f'Invalid "auth" argument: {auth!r}') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As I mentioned in the discord, here is one of the possible explanation lepture/authlib#357. Or |
Beta Was this translation helpful? Give feedback.
As I mentioned in the discord, here is one of the possible explanation lepture/authlib#357.
Or
UnsetType
get removed fromAuthTypes
when types were moved intohttpx/_types.py
since theauth
was able to beUnsetType
in https://github.com/encode/httpx/blame/34ba0e14b0d588860e408e6e21ec729af5667f7f/httpx/_client.py#L288