4

Steps I am following:

  1. /send_otp - Send otp to given number
  2. /token - generate access token if the provide number and otp are correct
  3. Authorize button - Need to provide the token to give access #Need help here
  4. /show_results - requires authorization to show results

I am using oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token") for authorization. The Authorize button is asking to enter username, password, client id and client secret.

How to test from swagger UI? I can use postman with Bearer token, but is it possible to use swagger UI for testing this? How to utilize the Authorize button to provide access to /show_results API?

/token: route look like

@app.post("/token")
def generate_token(phone_number: str, otp: str):
    ...

    # Return OAuth2 token
    return {"access_token": encoded_jwt, "token_type": "bearer"}

/Show_results: route looks like

@app.get("/show_results")
def show_results(token: Optional[str] = Depends(oauth2_scheme)):
    ...

enter image description here

Authorize button view:

enter image description here

Albina
  • 1,901
  • 3
  • 7
  • 19
Python coder
  • 743
  • 5
  • 18
  • Does this answer your question? [How to send Authorization Header through Swagger UI using FastAPI?](https://stackoverflow.com/questions/74085996/how-to-send-authorization-header-through-swagger-ui-using-fastapi) – Chris Apr 24 '23 at 16:08
  • You might find [this answer](https://stackoverflow.com/a/74268404/17865804) helpful as well. – Chris Apr 24 '23 at 16:08
  • @Chris, from this suggested [answer](https://stackoverflow.com/a/74088523/11844406), you mean to use `APIKeyHeader` instead of `OAuth2PasswordBearer`? – Python coder Apr 24 '23 at 16:40
  • @Chris I want to user `OAuth2` authentication, as I read that it is mostly used when there is subscription type authentication requirement, which is why I am using `OAuth2`. It is even fine for me, If I was able to enter phone number and otp using `Authorize` button, which is hitting `/token` url, which will also help in authorize I assume? – Python coder Apr 24 '23 at 16:46

0 Answers0