Bird
0
0

What is the main issue preventing the protected route from working correctly?

medium📝 Debug Q7 of 15
FastAPI - Authentication and Security
Examine the following FastAPI code snippet. What is the main issue preventing the protected route from working correctly? ```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 = Depends(oauth2_scheme)): return {"token": token} ```
AThe <code>OAuth2PasswordBearer</code> instance must be created inside the route function
BThe <code>Depends</code> function is missing parentheses
CThe route function must be synchronous, not asynchronous
DThe <code>tokenUrl</code> parameter should point to an actual token endpoint, not just "token"
Step-by-Step Solution
Solution:
  1. Step 1: Review OAuth2PasswordBearer usage

    The tokenUrl parameter should be a valid URL path where clients get tokens.
  2. Step 2: Check the provided tokenUrl

    Using just "token" is ambiguous and may not correspond to a real token endpoint.
  3. Step 3: Other options review

    Depends is correctly used with parentheses, async functions are allowed, and the instance can be global.
  4. Final Answer:

    The tokenUrl parameter should point to an actual token endpoint, not just "token" -> Option D
  5. Quick Check:

    tokenUrl must be a valid token endpoint path [OK]
Quick Trick: tokenUrl must be a valid token endpoint path [OK]
Common Mistakes:
MISTAKES
  • Assuming async route functions are invalid
  • Thinking Depends needs no parentheses
  • Creating OAuth2PasswordBearer inside route unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes