An admin account – but a token that can only read. In Lesson 43 of the FastAPI Complete Tutorial (Beginner → Deployment), we add OAuth2 scopes to our API, step by step, in a fresh, small async project with users, courses and the roles from Video 42. Roles vs scopes: a role belongs to the user, a scope belongs to the token. Then the code line by line: a list of scopes with descriptions, which scopes each role may ever get, and a login where the client asks for scopes (form.scopes) and the server grants only the intersection with the role – an unknown scope is a 400, and the answer says what was really granted. FastAPI's tools: OAuth2PasswordBearer(scopes=...), Security() – Depends with scopes – and SecurityScopes, which collects the scopes of the whole dependency chain. A get_current_user that checks the token's scopes AND the user's role on every request, and a 403 with WWW-Authenticate: Bearer error="insufficient_scope" (RFC 6750) – and why FastAPI's own tutorial uses 401. Demos: who gets which scopes, 5 tokens against 4 endpoints, a demoted teacher whose old token stops working, how scopes stack and cache, the Authorize dialog in Swagger with scope checkboxes – and 15 tests on SQLite and PostgreSQL. 💻 Code for this video: https://github.com/nileshborate/fastapi/tree/main/43-oauth2-scopes 📂 Full course code: https://github.com/nileshborate/fastapi ▶️ Full playlist: https://www.youtube.com/playlist?list=PLBjgbZvHwQmg 🌐 Website: https://www.nileshtechnology.in ⏱️ CHAPTERS 0:00 Intro 0:05 Homework answer (Video 42): course students, blocking users 1:41 A read-only token for a script 2:29 Today's plan 2:55 Where to get the code (GitHub) 3:32 Roles vs scopes 4:53 roles.py: SCOPES, ROLE_SCOPES, grant_scopes 6:24 Logging in with scopes 7:51 Who asks for what (demo) 8:47 Security() and SecurityScopes 10:33 get_current_user, line by line 12:01 401 or 403? insufficient_scope 13:06 Scopes on the endpoints and routers 14:17 Five tokens, four endpoints (demo) 15:32 The role still counts (demo) 16:10 How SecurityScopes collects scopes (demo) 17:30 Swagger: pick your scopes 18:19 Tests 19:41 3 common mistakes 20:16 Interview questions 21:13 Recap, homework & what's next 📝 HOMEWORK 1. A new scope "courses:delete" (admins only) and DELETE /courses/{course_id} that needs it 2. GET /me/token – the scopes of the token you're using right now, and when it expires (hint: a dependency that returns the decoded claims) 3. Tests for both The answer is in the next video! 🎯 INTERVIEW QUESTIONS IN THIS VIDEO • What are OAuth2 scopes? • How do you use scopes in FastAPI? • Roles or scopes – which one do you need? ⏭️ Next video: Email Verification and Password Reset – signed, expiring, one-time links 👍 Like, subscribe to Nilesh Technology and press the bell for the next lesson! #FastAPI #Python #OAuth2 #Scopes #Authorization #FastAPITutorial #BackendDevelopment #PythonTutorial #NileshTechnology
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.