question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to specify Token URL in swagger ui if you use multi OAuth2PasswordBearer

See original GitHub issue

First 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

admin_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl=path.join(settings.ROOT_PATH + "/auth/admin-token")
)
candidate_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl=path.join(settings.ROOT_PATH, "/auth/candidate-token")
)

Description

In my app I have two token urls for different user. So how can I specify one of them in swagger ui? Currently, swagger ui will use one of the token url based on the last oauth Depends you used in your code.

For example in my last router file I use depends candidate_oauth2_schema than the token url in swagger ui will become /auth/candidate-token but if I add another API below which uses depends admin_oauth2_schema then the token url will change to “/auth/admin-token”.

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

3.9.0

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
Dartt0ncommented, Mar 18, 2022

I have found the solution. You can specify different scheme_name for your OAuth2PasswordBearers.

Example:

admin_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl="/api/auth/admin-token",
    scheme_name="admin_oauth2_schema"
)
candidate_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl="/api/auth/candidate-token",
    scheme_name="candidate_oauth2_schema"
)

This way, Swagger UI will be able to different these two OAuth2PasswordBearers and will provide you useful UI to handle multiple oauth2 schemes. image image

1reaction
jiaulislamcommented, Dec 6, 2022

I have found the solution. You can specify different scheme_name for your OAuth2PasswordBearers.

Example:

admin_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl="/api/auth/admin-token",
    scheme_name="admin_oauth2_schema"
)
candidate_oauth2_schema = OAuth2PasswordBearer(
    tokenUrl="/api/auth/candidate-token",
    scheme_name="candidate_oauth2_schema"
)

This way, Swagger UI will be able to different these two OAuth2PasswordBearers and will provide you useful UI to handle multiple oauth2 schemes. image image

Thanks 👍🏻 Was scratching my head for hours lol.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OAuth 2.0 - Swagger
The flows object can specify multiple flows, but only one of each type. ... tokenUrl, The token URL to use for this flow....
Read more >
Security - First Steps - FastAPI
When we create an instance of the OAuth2PasswordBearer class we pass in the tokenUrl parameter. This parameter contains the URL that the client...
Read more >
Authorize button in swagger ui of FastAPI - Stack Overflow
What is the use of "Authorize" button and "/token" url both in swagger UI? I have already gone through the fastapi docs but...
Read more >
The Ultimate FastAPI Tutorial Part 10 - Auth via JSON Web ...
The Ultimate FastAPI Tutorial Part 10 - Auth via JSON Web Token (JWT). In part 10 of the FastAPI tutorial, we'll look at...
Read more >
Explaining FastAPI scopes - Lambert Labs
We recently started using a framework called FastAPI. ... so that developers can consume the APIs in a web interface: Swagger UI or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found