Bird
0
0

Identify the error in this FastAPI protected route code: ```python from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordBearer app = FastAPI() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") @app.get("/secure") async def secure_route(token: str = oauth2_scheme): return {"token": token} ```

medium📝 Debug Q6 of 15
FastAPI - Authentication and Security
Identify the error in this FastAPI protected route code: ```python from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordBearer app = FastAPI() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") @app.get("/secure") async def secure_route(token: str = oauth2_scheme): return {"token": token} ```
AFunction should not be async
BMissing Depends() around oauth2_scheme in function parameter
CRoute decorator syntax is incorrect
DOAuth2PasswordBearer should not be assigned to a variable
Step-by-Step Solution
Solution:
  1. Step 1: Check how dependencies are declared in FastAPI

    Dependencies must be wrapped with Depends() to be injected properly.
  2. Step 2: Identify missing Depends() usage

    The parameter token: str = oauth2_scheme is missing Depends(), so FastAPI won't treat it as a dependency.
  3. Final Answer:

    Missing Depends() around oauth2_scheme in function parameter -> Option B
  4. Quick Check:

    Dependency injection requires Depends() [OK]
Quick Trick: Always wrap dependencies with Depends() in route parameters [OK]
Common Mistakes:
MISTAKES
  • Forgetting Depends() around security schemes
  • Misunderstanding async function requirements
  • Incorrect route decorator usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes