Authenticating a Flask API Using JSON Web Tokens

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 187

  • @prettyprinted
    @prettyprinted  4 ปีที่แล้ว +7

    Join my free course on the basics of Flask-SQLAlchemy: prettyprinted.com/flasksql

  • @yusufsalk1136
    @yusufsalk1136 ปีที่แล้ว

    This is by far the most underrated jsonify pronunciation. Great video by the way. Thanks.

  • @marcoio8742
    @marcoio8742 4 ปีที่แล้ว

    Terrific! This is super useful. 3 years later, thank you

  • @pratikrane149
    @pratikrane149 5 ปีที่แล้ว +9

    Neat explanation. Everything straight to the point. Thanks a ton for sharing such quality tutorials! This helped me a lot in understanding jwt

  • @AnikSardar
    @AnikSardar 3 ปีที่แล้ว

    Best video on authentication. Extremely well explained.

  • @笨土豆
    @笨土豆 4 ปีที่แล้ว

    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

  • @mbonuchinedu2420
    @mbonuchinedu2420 4 ปีที่แล้ว +1

    ANOTHER LIFE SAVER......
    More blessing to you..................

  • @noletovictor
    @noletovictor 4 ปีที่แล้ว +3

    Perfect! That is exactly what I need! Thank you for this simple, fast and easy solution!

  • @WilliamRosswpr101
    @WilliamRosswpr101 7 ปีที่แล้ว +4

    Thanks for sharing this. It has been a huge help towards understanding JWT in Python

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +1

      You're welcome! Thanks for watching.

  • @koodauskanava9096
    @koodauskanava9096 7 ปีที่แล้ว +28

    Always use 401 for unauthenticated routes. 403 is forbidden logic, for example forbidden directory listing etc. Not related to authentication.

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +14

      Thanks!
      I should have looked those up before recording the video. I always get them confused.

  • @JulienPy
    @JulienPy 7 ปีที่แล้ว

    Still your great and step-by-step approach style. Very appreciated!

  • @TeddyAndersson95
    @TeddyAndersson95 7 ปีที่แล้ว

    So excited, to watch this! I also 100% sure you will deliver another awesome tutorial :D

  • @ThaEzioAuditore
    @ThaEzioAuditore 7 ปีที่แล้ว +2

    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.

  • @v4ldevrr4m47
    @v4ldevrr4m47 2 ปีที่แล้ว

    The best simple example. thanks

  • @koodauskanava9096
    @koodauskanava9096 7 ปีที่แล้ว +4

    Very good tutorial. Useful stuff!

  • @BRBallin1
    @BRBallin1 5 ปีที่แล้ว +1

    Great introduction. Made a somewhat intimidating topic seem straightforward

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Glad the video helped! Thanks for watching.

  • @rakeshingle
    @rakeshingle 5 ปีที่แล้ว +4

    Thank you, This helped me a lot.

  • @orlapa
    @orlapa 4 ปีที่แล้ว

    Excelente tutorial, this is useful for someone starting with this. Thanks

  • @saguoran
    @saguoran 5 ปีที่แล้ว +1

    2019, I found this video is useful. good contents.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I'm glad you like. Thanks for watching!

  • @techsteering
    @techsteering 3 ปีที่แล้ว

    Thanks a lot! This is exactly what I was looking for. You saved me tons of time. Thanks again. :)

  • @andrewbartlett2091
    @andrewbartlett2091 5 ปีที่แล้ว +1

    Really clear, very informative, thanks a lot

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @vic_shine
    @vic_shine 7 ปีที่แล้ว

    Many thanks, Anthony! Brain storage was improved :)

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +1

      You're welcome! Thanks for watching.

  • @ikhsanrahman9703
    @ikhsanrahman9703 5 ปีที่แล้ว +8

    cool, how about for refresh token. that happen when we try to keep user remain login inside

  • @somed_9216
    @somed_9216 8 หลายเดือนก่อน

    It was really helpful, thank you

  • @carloseduardodasilvaolivei8939
    @carloseduardodasilvaolivei8939 3 ปีที่แล้ว

    Thank you very much this video helped me a lot

  • @zzakpk
    @zzakpk 4 ปีที่แล้ว

    very helpful tutorial. Thank you.

  • @abhaychinchole3482
    @abhaychinchole3482 3 ปีที่แล้ว +2

    i am getting invalid token despite of copying the token...help will be appreciated

  • @robertwoodard8462
    @robertwoodard8462 3 ปีที่แล้ว +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 2 ปีที่แล้ว

      thank you very much, had the same error and your comment saved me a lot of time :)

    • @animeshmisra5387
      @animeshmisra5387 2 ปีที่แล้ว

      Thanks! man

    • @satriyaningjagat
      @satriyaningjagat ปีที่แล้ว

      data = jwt.decode(token, app.config['SECRET_KEY'], algorithms= ['HS512', 'HS256'])

  • @Leo-fh7fu
    @Leo-fh7fu 4 ปีที่แล้ว

    another beautiful video, thanks for sharing my friend, subscribed!!

  • @code_tech01
    @code_tech01 ปีที่แล้ว

    Thanks to explain

  • @charlieontheearth
    @charlieontheearth 7 ปีที่แล้ว

    Thanks for this video. It's very helpful for me.

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @СергейНаталенко-х2х
    @СергейНаталенко-х2х 4 ปีที่แล้ว

    Огромное спасибо
    Ваше видео очень помогло мне в работе)
    Thank you very much!!!

  • @christianduahmarfo5283
    @christianduahmarfo5283 ปีที่แล้ว +1

    How was the `prompt screen` possible? Did you use HTML for the Login form?

  • @Ekosusiloharjo
    @Ekosusiloharjo 5 ปีที่แล้ว +1

    Hi Anthony Get the code here not found

  • @Tbalbiza_POV
    @Tbalbiza_POV ปีที่แล้ว

    Thank you, very helpful !!!!!!!!!!!!!!!!!

    • @prettyprinted
      @prettyprinted  ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @PulkitKumar191
    @PulkitKumar191 7 ปีที่แล้ว

    Great tutorial. Thanks!

  • @george4061
    @george4061 ปีที่แล้ว

    thank you sir!

  • @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

  • @mellimelloweb
    @mellimelloweb 4 ปีที่แล้ว

    Thanks Anthony for your excellent work.
    Can you complete this video with the implementation of refresh token?
    That would be very great!

  • @gggal123
    @gggal123 5 ปีที่แล้ว

    Awesome Awesome Awesome Awesome Awesome Awesome Awesome tutorial.

  • @shreevidyas.r8275
    @shreevidyas.r8275 3 ปีที่แล้ว

    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.

  • @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 ?

  • @Maslacak1985
    @Maslacak1985 2 ปีที่แล้ว +1

    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 ปีที่แล้ว

      when returning decorated make sure its return decorated, not return decorated()

  • @fahrican9708
    @fahrican9708 4 ปีที่แล้ว

    great video thanks!

  • @XBoysGaming22
    @XBoysGaming22 6 ปีที่แล้ว

    This is nice! Great video!

  • @selman8653
    @selman8653 ปีที่แล้ว

    Thanks mate

  • @SanuKumar_addicted_brat
    @SanuKumar_addicted_brat 4 ปีที่แล้ว +1

    Thanks for sharing such a great video,
    can you also help me with the logout?

  • @danielfernandomurciaperdom7665
    @danielfernandomurciaperdom7665 4 ปีที่แล้ว

    thank you so much

  • @kingleo188
    @kingleo188 4 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      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?

  • @dhyanprasad5611
    @dhyanprasad5611 ปีที่แล้ว

    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

  • @bftechvideos
    @bftechvideos 3 ปีที่แล้ว

    Can you add LOGOUT route? For cleaning the Token

  • @priteshthaker8340
    @priteshthaker8340 5 ปีที่แล้ว

    Get the code link doesn't work

  • @Kinyanjui_765
    @Kinyanjui_765 2 ปีที่แล้ว

    hey if you see this...can make a video about how you can link this up with vanilla javascript? thank you

  • @eriklee1131
    @eriklee1131 5 ปีที่แล้ว

    Great video!

  • @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

  • @hsz7338
    @hsz7338 5 ปีที่แล้ว +1

    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.

  • @petrk.4901
    @petrk.4901 5 ปีที่แล้ว +1

    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

  • @TusharBhut-mk6mn
    @TusharBhut-mk6mn 2 ปีที่แล้ว

    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.

  • @asgaraliyev8740
    @asgaraliyev8740 4 ปีที่แล้ว

    is that request being restful request by the secret key now?

  • @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

  • @surajthapa5688
    @surajthapa5688 2 ปีที่แล้ว

    Can anyone provide a link for a next video please?

  • @nicolascorrea1989
    @nicolascorrea1989 4 ปีที่แล้ว

    you're the best !

  • @fogcc1015
    @fogcc1015 ปีที่แล้ว

    but how automatically pass this token in all request ? Is that THE question !!!

  • @animeshmishra4282
    @animeshmishra4282 3 ปีที่แล้ว

    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 ? 😩

  • @sunilsolanki5043
    @sunilsolanki5043 2 ปีที่แล้ว

    where the data variable is used?

  • @KillerCookie
    @KillerCookie 4 ปีที่แล้ว

    Thanks for a great tutorial!
    However, the link to the code seems to be broken, might want to fix it.

  • @MegaFeel1
    @MegaFeel1 4 ปีที่แล้ว

    Hey bro! But where is the code from this video? "Get the code here" page give me 404 error

  • @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...

  • @mahdihoseyni3661
    @mahdihoseyni3661 2 ปีที่แล้ว

    how can get refresh token in this case?

  • @rienadetrof6198
    @rienadetrof6198 5 ปีที่แล้ว

    Hello , how do you get the pop up to sign in? Great video btw!

    • @LatinDanceVideos
      @LatinDanceVideos 5 ปีที่แล้ว +1

      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)

  • @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 ปีที่แล้ว +1

      You may have to. I'll have to test this out with calling from Python.

  • @fly50789
    @fly50789 ปีที่แล้ว

    I am not sure about what is the different about flask-jwt-extend and flask-login? Which is better?

  • @ambeshsingh1251
    @ambeshsingh1251 6 ปีที่แล้ว

    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..

  • @ComputerScience101
    @ComputerScience101 5 ปีที่แล้ว

    Hi , I have created database using sqlite, how do i access the data here for credentials

  • @FlexThoseMuscles
    @FlexThoseMuscles 6 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      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.

  • @moiarmio4863
    @moiarmio4863 4 ปีที่แล้ว

    how to use it own my website rather than local host ?

  • @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

  • @mrsterbenblack9270
    @mrsterbenblack9270 5 ปีที่แล้ว

    Very nice.

  • @JosephR9
    @JosephR9 7 ปีที่แล้ว

    what do say at @9:50 ? the token its generated in....?¿

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว

      The token is generated in bytes. I decode to a string so I can use it as a string.

  • @danieldaschle
    @danieldaschle 7 ปีที่แล้ว +2

    THANKS

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @sunilsolanki5043
    @sunilsolanki5043 2 ปีที่แล้ว

    how to pass token directly to another api?

  • @biunircloud6432
    @biunircloud6432 4 ปีที่แล้ว

    Someone could share link the next video? Thanks

  • @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?

  • @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?

  • @KotoFF4ik
    @KotoFF4ik 3 ปีที่แล้ว

    Awesome

  • @castro_hassler
    @castro_hassler 5 ปีที่แล้ว

    What is the difference between doing this, and using the flask session object?

    • @castro_hassler
      @castro_hassler 5 ปีที่แล้ว

      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 ¿

  • @aloysiusseow9774
    @aloysiusseow9774 3 ปีที่แล้ว

    request.authorization does not show a pop up authentication on my web browser pls help!

  • @____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.

  • @uripeled2
    @uripeled2 4 ปีที่แล้ว

    How the the token is saved?

  • @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 ?

  • @anusham4559
    @anusham4559 6 ปีที่แล้ว

    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  6 ปีที่แล้ว

      Could you be more specific? What are you trying to accomplish?

    • @anusham4559
      @anusham4559 6 ปีที่แล้ว

      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!

  • @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

  • @naveengoyal5243
    @naveengoyal5243 6 ปีที่แล้ว

    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  6 ปีที่แล้ว

      You can use the library requests and authenticate that way.

  • @SwapnilSudhir47
    @SwapnilSudhir47 6 ปีที่แล้ว

    Hi , I am trying to login in the first login function, but it is again showing the login popup, even after credentials. Please help

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      Even if you use the code I've linked to in the description?

    • @SwapnilSudhir47
      @SwapnilSudhir47 6 ปีที่แล้ว

      Its done...found a different approach.

    • @ayah5896
      @ayah5896 6 ปีที่แล้ว

      @@SwapnilSudhir47 I have got the same issue. Can you please tell me how did you solve it? Thanks

    • @SwapnilSudhir47
      @SwapnilSudhir47 6 ปีที่แล้ว

      @@ayah5896 I might have forgotten what i was doing, but can you please check in the console network, what issue is there, I can help :-)

  • @Coristo05
    @Coristo05 4 ปีที่แล้ว

    Hi, i've got problem with request.authentication cause it's NoneType object. How should I fix it?

    • @corollayanki
      @corollayanki 3 ปีที่แล้ว +1

      it's request.authorization, but you probably figured that out by now.

  • @kastriotdreshaj6299
    @kastriotdreshaj6299 7 ปีที่แล้ว

    would be better to put token on header and not in url ?

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +2

      Yes, I'm going to cover that in the next video with a realistic example. I wanted to keep things as simple as possible in this one to only explain the concepts.

    • @kastriotdreshaj6299
      @kastriotdreshaj6299 7 ปีที่แล้ว

      ok perfect i'm learning flask too what is your opinion on flask compare with django ?

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +1

      Flask doesn't force you to do anything, so if you enjoy the extra freedom and know exactly what you want to do, Flask is great.
      If you are building an app where you are using a lot of standard features and you don't mind Django's approach, you can try out Django.

  • @lovneetchugh8063
    @lovneetchugh8063 3 ปีที่แล้ว

    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})

  • @AnopSinghRanawatperl
    @AnopSinghRanawatperl 4 ปีที่แล้ว

    how to destroy jwt token on logout?

  • @TheNorthRemember
    @TheNorthRemember 7 ปีที่แล้ว

    Great tutorial, can you please give me the syntax theme

    • @prettyprinted
      @prettyprinted  7 ปีที่แล้ว +1

      Thanks. I just use the default one in Sublime Text 3.

  • @schoolive7977
    @schoolive7977 4 ปีที่แล้ว

    request.authorization is giving None? Help

    • @Coristo05
      @Coristo05 4 ปีที่แล้ว

      Did You fixed it? I've the same problem :/