OAuth2: Header issue
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
async def get_current_user(
token: str = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database)
):
credentials_exception = HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"}
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
token_data = TokenPlayload(email=email)
except JWTError:
raise credentials_exception
dbuser = await get_user_by_email(db, token_data.email)
if not dbuser:
raise credentials_exception
user = User(**dbuser.dict(), token=token)
return user
Description
I follow the documentation for security part. The authorization works, however, when I want to use it as dependencies for other routes. I got this error.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.68.0
Python Version
3.8.3
Additional Context
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
OAuth 2.0 Authorization Header - Stack Overflow
I have checked the differences between OAuth 1.0 & 2.0, and I have some confusion on Authorization Header (1.0 and 2.0), OAuth 1.0...
Read more >Making Authenticated Requests - OAuth 2.0 Simplified
The access token is sent to the service in the HTTP Authorization header prefixed by the text Bearer . Historically, some services allowed...
Read more >Authorization header is not passed · Issue #1343 - GitHub
As I said, my config works, only Chrome (also Safari) does not show the header. But that's another issue nothing to do with...
Read more >Trying to get OAuth 2.0 token, receiving Missing valid ...
Incidentally, I'm using a Go library to help with OAuth2, and I also got the same error Missing valid authorization header error, then...
Read more >CORS error when posting to /oauth2/token | FusionAuth Forum
When I use Postman, there is no CORS issue and this POST request to /oauth2/token works and I get valid Access and ID...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Kludex thanks for your recommendation. I changed the import statements and it worked.
You’re welcome. Would you mind closing the issue? 😗
For reference, FastAPI creates its own
HTTPException
, inheriting from StarletteHTTPException
: https://github.com/tiangolo/fastapi/blob/25a48287ed6043f7ee81417cef3c4d1f1dc56f72/fastapi/exceptions.py#L8-L16