Тёмный

Authenticating a Flask API Using JSON Web Tokens 

Pretty Printed
Подписаться 96 тыс.
Просмотров 174 тыс.
50% 1

In this video, I show you how to use JSON Web Tokens (JWT) to authenticate users of your API. First, a user will have to login using a specially created route which returns a token. Once a token is generated, it can be sent along with the rest of the request to other parts of your API to verify the user's identity without the user having to provide any additional login information. It's an easy way to verify the identity of users without making them go through the extra step of logging in every time they make a request.
Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted....
Join my free course on the basics of Flask-SQLAlchemy: prettyprinted....
Basic HTTP Auth video: • How to Use HTTP Basic ...
Intro to JWT video: • An Introduction to JSO...

Опубликовано:

 

21 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 187   
@prettyprinted
@prettyprinted 4 года назад
Join my free course on the basics of Flask-SQLAlchemy: prettyprinted.com/flasksql
@prettyprinted
@prettyprinted 7 лет назад
Thanks for watching! Join the hundreds of others who have taken my Flask courses on prettyprinted.com
@stanleyjohn4670
@stanleyjohn4670 4 года назад
Can you show how to use JWT in Django? It's a sincere request, thank you.
@yusufsalk1136
@yusufsalk1136 11 месяцев назад
This is by far the most underrated jsonify pronunciation. Great video by the way. Thanks.
@pratikrane149
@pratikrane149 5 лет назад
Neat explanation. Everything straight to the point. Thanks a ton for sharing such quality tutorials! This helped me a lot in understanding jwt
@prettyprinted
@prettyprinted 5 лет назад
I'm glad I could help.
@koodauskanava9096
@koodauskanava9096 7 лет назад
Always use 401 for unauthenticated routes. 403 is forbidden logic, for example forbidden directory listing etc. Not related to authentication.
@prettyprinted
@prettyprinted 7 лет назад
Thanks! I should have looked those up before recording the video. I always get them confused.
@marcoio8742
@marcoio8742 3 года назад
Terrific! This is super useful. 3 years later, thank you
@AnikSardar
@AnikSardar 3 года назад
Best video on authentication. Extremely well explained.
@mbonuchinedu2420
@mbonuchinedu2420 3 года назад
ANOTHER LIFE SAVER...... More blessing to you..................
@user-qs7ox9kd1q
@user-qs7ox9kd1q 3 года назад
Thx for this video, i have learned several JWT video tutorial on youtube. Your video is most clear i ever seen these days. Very helpful thx
@noletovictor
@noletovictor 3 года назад
Perfect! That is exactly what I need! Thank you for this simple, fast and easy solution!
@prettyprinted
@prettyprinted 3 года назад
Glad it helped!
@WilliamRosswpr101
@WilliamRosswpr101 7 лет назад
Thanks for sharing this. It has been a huge help towards understanding JWT in Python
@prettyprinted
@prettyprinted 7 лет назад
You're welcome! Thanks for watching.
@JulienPy
@JulienPy 7 лет назад
Still your great and step-by-step approach style. Very appreciated!
@prettyprinted
@prettyprinted 7 лет назад
I'm glad you enjoyed it!
@abhaychinchole3482
@abhaychinchole3482 3 года назад
i am getting invalid token despite of copying the token...help will be appreciated
@TeddyAndersson95
@TeddyAndersson95 7 лет назад
So excited, to watch this! I also 100% sure you will deliver another awesome tutorial :D
@prettyprinted
@prettyprinted 7 лет назад
I try my best!
@rakeshingle
@rakeshingle 4 года назад
Thank you, This helped me a lot.
@koodauskanava9096
@koodauskanava9096 7 лет назад
Very good tutorial. Useful stuff!
@ikhsanrahman9703
@ikhsanrahman9703 5 лет назад
cool, how about for refresh token. that happen when we try to keep user remain login inside
@dhyanprasad5611
@dhyanprasad5611 11 месяцев назад
nice video. wanted to add that when i tried to authenticate the token using a header, I got back invalid token because I didnt specify the decode algorithm
@v4ldevrr4m47
@v4ldevrr4m47 2 года назад
The best simple example. thanks
@robertwoodard8462
@robertwoodard8462 2 года назад
The jwt decode always throws an exception to token is invalid. It's passing the correct token, checked with a print, but it doesn't seem to like it. *UPDATE* you now have to pass the algorithm to decode the jwt token with as a list
@robertkl5261
@robertkl5261 Год назад
thank you very much, had the same error and your comment saved me a lot of time :)
@animeshmisra5387
@animeshmisra5387 Год назад
Thanks! man
@satriyaningjagat
@satriyaningjagat Год назад
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms= ['HS512', 'HS256'])
@ThaEzioAuditore
@ThaEzioAuditore 7 лет назад
Thank you thank you thank you a million times ! your efforts are very much appreciated
@prettyprinted
@prettyprinted 7 лет назад
You're welcome! And I appreciate you for taking the time to watch.
@somed_9216
@somed_9216 3 месяца назад
It was really helpful, thank you
@BRBallin1
@BRBallin1 5 лет назад
Great introduction. Made a somewhat intimidating topic seem straightforward
@prettyprinted
@prettyprinted 5 лет назад
Glad the video helped! Thanks for watching.
@andrewbartlett2091
@andrewbartlett2091 5 лет назад
Really clear, very informative, thanks a lot
@prettyprinted
@prettyprinted 5 лет назад
You're welcome! Thanks for watching.
@orlapa
@orlapa 3 года назад
Excelente tutorial, this is useful for someone starting with this. Thanks
@shreevidyas.r8275
@shreevidyas.r8275 2 года назад
Thankyou for this wonderful video... Now am able to know how to create token and storing them...Can you please provide the video on requesting using that token from that moment on.
@saguoran
@saguoran 5 лет назад
2019, I found this video is useful. good contents.
@prettyprinted
@prettyprinted 5 лет назад
I'm glad you like. Thanks for watching!
@vic_shine
@vic_shine 7 лет назад
Many thanks, Anthony! Brain storage was improved :)
@prettyprinted
@prettyprinted 7 лет назад
You're welcome! Thanks for watching.
@mellimelloweb
@mellimelloweb 3 года назад
Thanks Anthony for your excellent work. Can you complete this video with the implementation of refresh token? That would be very great!
@techsteering
@techsteering 3 года назад
Thanks a lot! This is exactly what I was looking for. You saved me tons of time. Thanks again. :)
@carloseduardodasilvaolivei8939
@carloseduardodasilvaolivei8939 3 года назад
Thank you very much this video helped me a lot
@zzakpk
@zzakpk 3 года назад
very helpful tutorial. Thank you.
@Tbalbiza_POV
@Tbalbiza_POV Год назад
Thank you, very helpful !!!!!!!!!!!!!!!!!
@prettyprinted
@prettyprinted Год назад
You're welcome! Thanks for watching.
@twishasahay3178
@twishasahay3178 2 года назад
Hi i dont know why but the auth =request.authorization is not working for me. I am not getting the notification to fill in the username/password. I am getting the could not verify response. do i have to install any library other than the ones mentioned in the video or change any settings
@Leo-fh7fu
@Leo-fh7fu 4 года назад
another beautiful video, thanks for sharing my friend, subscribed!!
@code_tech01
@code_tech01 11 месяцев назад
Thanks to explain
@Maslacak1985
@Maslacak1985 2 года назад
any guess with : RuntimeError: Working outside of request context. This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.
@paballo694
@paballo694 Год назад
Did you manage to solve this?
@sosajacobb7973
@sosajacobb7973 10 месяцев назад
when returning decorated make sure its return decorated, not return decorated()
@gggal123
@gggal123 4 года назад
Awesome Awesome Awesome Awesome Awesome Awesome Awesome tutorial.
@user-wl2iq7zm8r
@user-wl2iq7zm8r 3 года назад
Огромное спасибо Ваше видео очень помогло мне в работе) Thank you very much!!!
@charlieontheearth
@charlieontheearth 7 лет назад
Thanks for this video. It's very helpful for me.
@prettyprinted
@prettyprinted 7 лет назад
You're welcome! Thanks for watching.
@george4061
@george4061 Год назад
thank you sir!
@PulkitKumar191
@PulkitKumar191 7 лет назад
Great tutorial. Thanks!
@prettyprinted
@prettyprinted 7 лет назад
Thanks, I appreciate it!
@selman8653
@selman8653 Год назад
Thanks mate
@christianduahmarfo5283
@christianduahmarfo5283 Год назад
How was the `prompt screen` possible? Did you use HTML for the Login form?
@fahrican9708
@fahrican9708 4 года назад
great video thanks!
@Ekosusiloharjo
@Ekosusiloharjo 5 лет назад
Hi Anthony Get the code here not found
@raghu11220
@raghu11220 4 года назад
Nice man.. your doing great job.. its possible to make video on Keycloak with flask or django.. looking forward to hear from you
@XBoysGaming22
@XBoysGaming22 6 лет назад
This is nice! Great video!
@prettyprinted
@prettyprinted 6 лет назад
Thanks for watching!
@TusharBhut-mk6mn
@TusharBhut-mk6mn Год назад
Can you make video on individual authentication for user and admin, where user authorized can use only [GET] method, while admin authorized can use [crud] methods.
@r5bc
@r5bc 4 года назад
Hi, very helpful video. Please can you put the link to the video that follows this one in this flask api cours ?
@FlexThoseMuscles
@FlexThoseMuscles 6 лет назад
I encountered problems while running, it always says no module name jwt so I did steps below: make sure to upgrade flask to the last version with : pip3 install flask uninstall jwt with: pip3 uninstall jwt install pyjwt with: pip3 install pyjwt
@FlexThoseMuscles
@FlexThoseMuscles 6 лет назад
thanks for the like! Our teacher is sending his students on your tutorial (see: jberger.org/ on the twitter feed). Best luck with the premium courses and the tutoring.
@SanuKumar_addicted_brat
@SanuKumar_addicted_brat 3 года назад
Thanks for sharing such a great video, can you also help me with the logout?
@Kinyanjui_765
@Kinyanjui_765 Год назад
hey if you see this...can make a video about how you can link this up with vanilla javascript? thank you
@anishupadhyay898
@anishupadhyay898 2 года назад
hi when with the valid token obtained from login page still I get the message as token is missing.. can someone please help me with this
@danielfernandomurciaperdom7665
@danielfernandomurciaperdom7665 3 года назад
thank you so much
@eriklee1131
@eriklee1131 5 лет назад
Great video!
@prettyprinted
@prettyprinted 5 лет назад
Thanks for watching!
@nicolascorrea1989
@nicolascorrea1989 4 года назад
you're the best !
@hsz7338
@hsz7338 5 лет назад
Thank you for putting amazing tutorials together. I was wondering if you could make a video on Flask or Flask_restplus uses Oauth2? Thank you.
@prettyprinted
@prettyprinted 5 лет назад
I'll consider that. Thanks for the idea and for watching.
@fogcc1015
@fogcc1015 Год назад
but how automatically pass this token in all request ? Is that THE question !!!
@shivanshjayara6372
@shivanshjayara6372 2 года назад
why pop up window for login is not opening. Can any one please tell me that. Any html template needed for that?
@surajthapa5688
@surajthapa5688 2 года назад
Can anyone provide a link for a next video please?
@petrk.4901
@petrk.4901 4 года назад
Hello, I would like to thank you for the videos you make are very beneficial for me. I wanted to ask you if you could create user roles using token_required. In this video, you used the feature only to reveal content for the logged-in user. Couldn't the functionality be extended to user roles? For example, Admin would have read, write, user read only, or other read only for some content. I want to create user roles. Thank you
@KillerCookie
@KillerCookie 4 года назад
Thanks for a great tutorial! However, the link to the code seems to be broken, might want to fix it.
@madhavparikh9653
@madhavparikh9653 4 года назад
Really liked the explanation !!!. Still, it directly not verified and does not display any prompt to enter username and password. Anyone know why?
@fly50789
@fly50789 Год назад
I am not sure about what is the different about flask-jwt-extend and flask-login? Which is better?
@animeshmishra4282
@animeshmishra4282 2 года назад
If i am making a website that uses this api but i want users to surf the website but not that api then what should i do ? 😩
@hrishinani
@hrishinani 2 года назад
Hi very clean explanation of the code but I'm unable to get output due to this line try: data = jwt.decode(token, app.config['SECRET_KEY']) any resolution for this please...
@rizwanrizwan4176
@rizwanrizwan4176 5 лет назад
how can i validate the different type of user like admin can view everything but user must restricted to see something? and also how to write security code for form data ?
@ComputerScience101
@ComputerScience101 5 лет назад
Hi , I have created database using sqlite, how do i access the data here for credentials
@jacksonjegatheesan5045
@jacksonjegatheesan5045 4 года назад
hi nice tutorial by the way how would i log out and how can i send login credentials from my front end
@ambeshsingh1251
@ambeshsingh1251 5 лет назад
very nice video. After hiting the login url for the first time it asks for username and password but after that it does not ask for it and directly gives the token. can u help me how it should ask for user-id and password everytime login API is hit..
@priteshthaker8340
@priteshthaker8340 4 года назад
Get the code link doesn't work
@____R__
@____R__ 5 лет назад
After hitting login api for first time it asks for username and password. But after that it does not ask and direct given new token . Can u help me how it should ask userid and password every time login api is hit.
@sunilsolanki5043
@sunilsolanki5043 2 года назад
how to pass token directly to another api?
@mrsterbenblack9270
@mrsterbenblack9270 5 лет назад
Very nice.
@prettyprinted
@prettyprinted 5 лет назад
Thanks for watching!
@mahdihoseyni3661
@mahdihoseyni3661 2 года назад
how can get refresh token in this case?
@moiarmio4863
@moiarmio4863 4 года назад
how to use it own my website rather than local host ?
@sunilsolanki5043
@sunilsolanki5043 2 года назад
where the data variable is used?
@milindchavan4329
@milindchavan4329 3 года назад
this video belongs to which play list ? coz i want cover the whole topic ? plz suggest #PrettyPrinted
@MegaFeel1
@MegaFeel1 3 года назад
Hey bro! But where is the code from this video? "Get the code here" page give me 404 error
@kmishy
@kmishy 2 года назад
Great explanation sir but title is misguiding because you have not used any API
@Brunovtf
@Brunovtf 3 года назад
Can you add LOGOUT route? For cleaning the Token
@aloysiusseow9774
@aloysiusseow9774 3 года назад
request.authorization does not show a pop up authentication on my web browser pls help!
@alfredsyriac
@alfredsyriac 2 года назад
how to verify token from firebase?
@biunircloud6432
@biunircloud6432 4 года назад
Someone could share link the next video? Thanks
@danieldaschle
@danieldaschle 7 лет назад
THANKS
@prettyprinted
@prettyprinted 7 лет назад
You're welcome! Thanks for watching.
@georgegoldmanonyedikachijo9330
@georgegoldmanonyedikachijo9330 3 года назад
while trying to implement this i got TypeError: 'module' object is not callable Traceback (most recent call last) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/home/lb/Documents/trie/venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/lb/Documents/trie/application/admin/routes/auth.py", line 20, in unprotected token = jwt({'user': auth.username, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.secret_key) TypeError: 'module' object is not callable
@artisole3304
@artisole3304 2 года назад
same bro same error
@KotoFF4ik
@KotoFF4ik 3 года назад
Awesome
@asgaraliyev8740
@asgaraliyev8740 4 года назад
is that request being restful request by the secret key now?
@ayyapanr
@ayyapanr 5 лет назад
Great Tutorial! Works on the browser and postman.. but when I try Python3 requests, I get 401 back.. wireshark captures look similar.. anyways do we need to consider some encoding when we use Python3 clients?
@prettyprinted
@prettyprinted 5 лет назад
You may have to. I'll have to test this out with calling from Python.
@rienadetrof6198
@rienadetrof6198 4 года назад
Hello , how do you get the pop up to sign in? Great video btw!
@LatinDanceVideos
@LatinDanceVideos 4 года назад
this is the section of code that generates the pop box to sign in. maybe try using chrome incognito window. as the standard chrome caching introduces confounding behaviour. def login(): print("def login()") auth = request.authorization print("auth:", auth)
@lovneetchugh8063
@lovneetchugh8063 2 года назад
I am getting this error return jsonify({'token' : token.decode('UTF-8')}) AttributeError: 'str' object has no attribute 'decode'
@FailedSquare
@FailedSquare 2 года назад
Read the error message. Its telling you token is already a string type. In his example, on an older version of jwt it returned a byte type. Which is why the decode was needed for his example. return jsonify({'token': token})
@naveengoyal5243
@naveengoyal5243 5 лет назад
Can you please help me, that how should I go to the protected view without copy pasting the token manually and only using the python code.
@prettyprinted
@prettyprinted 5 лет назад
You can use the library requests and authenticate that way.
@castro_hassler
@castro_hassler 4 года назад
What is the difference between doing this, and using the flask session object?
@castro_hassler
@castro_hassler 4 года назад
Just in the first case the clients does manage the request token to access the server, and in the other case the server allows a client based on a Cookie session, which one is better? Scalability seems to be in troubles when the server is the one who manage the permissions 'cause it has to maintain a state, but when is it a good idea to use, I mean the flask session object ¿
@kingleo188
@kingleo188 3 года назад
Thank you very much for the great video, Very well explained. Although I have stumbled at 17:49, Even though I have added the token in the URL, I got a "token is invalid" json response, Please help, I have gone through and copied your code as it is, But not sure where I went wrong.
@gustavocarnaval6622
@gustavocarnaval6622 3 года назад
Try to specify the algorithm on the decode function of JWT.
@selimduvakl6807
@selimduvakl6807 2 года назад
ı have the same problem. Did you be able to solve it?
@uripeled2
@uripeled2 4 года назад
How the the token is saved?
@anusham4559
@anusham4559 5 лет назад
I am new to python and its Very helpful. Can you let me know how to pass a parameter to 'protected' function along with token?
@prettyprinted
@prettyprinted 5 лет назад
Could you be more specific? What are you trying to accomplish?
@anusham4559
@anusham4559 5 лет назад
never mind. I got it working. I actually had an input parameter to the function and passing token along with input parameter was not working. But the issue was I was not calling it properly. Its working now. Thanks!
@Coristo05
@Coristo05 4 года назад
Hi, i've got problem with request.authentication cause it's NoneType object. How should I fix it?
@corollayanki
@corollayanki 3 года назад
it's request.authorization, but you probably figured that out by now.
@nns.saidinesh
@nns.saidinesh 5 месяцев назад
Share the code for us
@asimhafeezz
@asimhafeezz 4 года назад
is it flask-jwt installed?? becuz it says no library exists when i try to install jwt...
@corollayanki
@corollayanki 3 года назад
the package is called pyjwt, so you do: pip install pyjwt
Далее
Italians vs @BayashiTV_  SO CLOSE
00:30
Просмотров 4,2 млн
КТО ЛЮБИТ ГРИБЫ?? #shorts
00:24
Просмотров 1,1 млн
What Is JWT and Why Should You Use JWT
14:53
Просмотров 1,1 млн
Python REST API Tutorial - Building a Flask REST API
1:14:01
Authentication and Authorization With Flask-Login
35:20
Custom Authentication (JSON Web Tokens)
13:03
Просмотров 16 тыс.
Difference between cookies, session and tokens
11:53
Просмотров 617 тыс.
Session Vs JWT: The Differences You May Not Know!
7:00
Build your first REST API with Flask and PostgreSQL
27:42