WEBVTT 00:00.450 --> 00:08.040 So let's say that the user next six wants to access the API, like maybe they want to go create a new 00:08.040 --> 00:12.500 to do item, they want to, you know, list out there to DOS, whatever it is. 00:12.510 --> 00:18.210 Let's go ahead and first log in as Nick Sixing create to do so that we don't have to do the full Kearl 00:18.210 --> 00:19.550 thing for creating it to do so. 00:19.570 --> 00:20.670 Going to log out here. 00:21.540 --> 00:23.850 Now it has already working for us. 00:23.880 --> 00:25.770 Let's go back to the home page. 00:26.560 --> 00:26.940 There we go. 00:26.940 --> 00:30.540 Now I've properly logged outlet's log in. 00:31.050 --> 00:32.850 I'm going to be Nick six. 00:33.930 --> 00:35.790 Do my classic password. 00:36.060 --> 00:38.670 I'm going to create to do that just says hello. 00:38.910 --> 00:39.990 That's very important. 00:40.260 --> 00:40.700 Save. 00:41.230 --> 00:41.570 OK. 00:42.000 --> 00:45.240 Now that I have this here, I want to be able to see this. 00:45.270 --> 00:45.870 Hello. 00:46.650 --> 00:47.400 Via the API. 00:47.430 --> 00:51.990 And in fact, if you'll notice, if we go to a new tab here and I go to local host slash, you know, 00:52.020 --> 00:58.680 API slash tatoos, we get a big air here, even though I've logged in as Nick if I try to come here 00:58.680 --> 01:00.030 and see what we've got. 01:00.420 --> 01:02.700 It says, hey, you have not provided me a token. 01:02.730 --> 01:04.410 I cannot show you anything here. 01:04.830 --> 01:09.930 Now, if you do want to change this so that you're actually able to still see it here, if we come back 01:09.930 --> 01:16.200 to our settings dot pi, we can add another authentication class, which is we can accept the token 01:16.200 --> 01:22.500 authentication or session authentication, which is basically what we use inside of a browser. 01:22.530 --> 01:27.360 So just by adding this line, if you go ahead and reload this page, you'll notice, hey, it shows 01:27.360 --> 01:30.000 the next six and that's all back to normal. 01:30.060 --> 01:31.800 But that's besides the point. 01:31.800 --> 01:32.520 We want to start. 01:32.610 --> 01:38.310 You know, if we take this to Dew's and, you know, we do a Kerl for this, let's go ahead and do a 01:38.310 --> 01:38.910 Kerl. 01:39.910 --> 01:41.220 For the following, right? 01:41.260 --> 01:45.640 It's going to say, hey, you've not provided some the proper authentication credentials. 01:46.990 --> 01:52.260 And so if we want to fix this and use that token that we created, we can do the Kerl and we're going 01:52.260 --> 01:54.310 to pass in in the header. 01:54.310 --> 01:58.300 Same thing that we did when we said, you know, that we're looking for the application, Jason. 01:58.870 --> 02:00.190 We're gonna do a dash. 02:00.700 --> 02:03.310 And then here we want to specify capital H. 02:03.430 --> 02:10.780 And then inside of the string capital off authorization, you got to really spell it out there. 02:10.780 --> 02:15.610 Authorization, Colon Space, Tolkan Space. 02:15.730 --> 02:17.770 And then we're going to provide our token there. 02:17.770 --> 02:19.510 So it's really important authorization. 02:19.570 --> 02:21.280 Colon space, tokens, space. 02:21.710 --> 02:25.350 And then let's go ahead and move over to our admen. 02:26.070 --> 02:26.440 Okay. 02:26.890 --> 02:28.270 So I'm going to copy. 02:29.820 --> 02:33.390 This string of letters and numbers copy this. 02:34.460 --> 02:36.790 Come back, paste this in here. 02:36.970 --> 02:38.260 And when I hit enter. 02:38.860 --> 02:39.820 Look at that. 02:40.090 --> 02:42.310 We get the proper information. 02:42.760 --> 02:47.220 We get this helo that I had just created just by providing this Tokino. 02:47.270 --> 02:53.050 And I can use this token to any of the things, create stuff, you know, delete, update, whatever 02:53.050 --> 02:54.010 it is that we want. 02:54.400 --> 03:00.130 And I want to point out here is that, you know, if we try to do the classic authentication, the basic 03:00.130 --> 03:08.050 authentication, if I do you and I say, you know, I want to provide Nick six and the password just 03:08.050 --> 03:08.830 like this. 03:09.820 --> 03:11.830 This is not going to be accepted. 03:12.040 --> 03:15.550 We have said we can only do session or token authentication. 03:16.000 --> 03:19.270 Now, this brings up a situation where, you know what? 03:19.270 --> 03:27.190 If someone already has a user account but they want to go get what their token is or even, for example, 03:27.190 --> 03:29.350 Nick at six, he already got a token. 03:29.350 --> 03:35.380 But let's say, you know, the application has been deleted off the user's phone and they need to install 03:35.380 --> 03:36.460 again for some reason. 03:37.300 --> 03:39.880 Nick six needs to get their token. 03:40.180 --> 03:41.440 How do we get that back? 03:41.470 --> 03:42.760 They don't sign up again. 03:42.760 --> 03:45.890 If they try and sign up again, will say, hey, that usernames already token. 03:46.360 --> 03:50.830 Well, we need to do is have a u r l where the user can get that token. 03:51.010 --> 03:54.310 Now, for me, I think the proper name for that is log in. 03:55.090 --> 03:59.470 Some people argue that it should be, you know, get token, generate token, whatever it is. 03:59.890 --> 04:04.730 But for me, I think it sort of signifies that you're, quote unquote logging in via the API. 04:04.780 --> 04:09.160 You're trying to get that token to say that you can now interact with the API. 04:09.370 --> 04:13.060 So let's go ahead and move back to Adam and let's go to our your URLs. 04:14.320 --> 04:16.120 And I'm going to specify here. 04:16.450 --> 04:22.100 This will just sort of help keep things organized, I'm going to say that this is our to DOS and that 04:22.150 --> 04:25.480 this down here is our API. 04:26.120 --> 04:27.970 This is our authentication. 04:28.390 --> 04:29.830 Let's just keep it off. 04:30.730 --> 04:36.570 And I'm going to copy this path and I'm going to create one for Log-in, which again, all this means 04:36.600 --> 04:38.970 is that it's going to get a token. 04:39.150 --> 04:41.360 So we'll go ahead and save this. 04:41.700 --> 04:47.640 Come back to our views and I'm going to copy everything that we have for the sign up, including that 04:47.960 --> 04:49.760 over R.F. Exempting. 04:49.780 --> 04:56.640 We want the same situation when someone is going to be logging in, a.k.a. getting their tokens. 04:56.640 --> 04:58.260 So let's change this to log in. 04:58.710 --> 05:01.830 Now, there is some changing that we have to do with the code here. 05:01.840 --> 05:03.630 So we still want to make sure that it's a post. 05:03.810 --> 05:05.640 That's definitely true. 05:06.000 --> 05:08.490 We don't want to do this try except thing. 05:09.300 --> 05:11.040 We are going to use one of those eventually. 05:11.040 --> 05:15.330 But for the time being, let's just go ahead and get the data, which that's going to be the same as 05:15.330 --> 05:15.780 last time. 05:15.780 --> 05:17.360 We're gonna use that Jason parser. 05:17.910 --> 05:20.040 Get that information in this time. 05:20.340 --> 05:25.770 We're gonna try and get the user object using the authenticate function. 05:25.920 --> 05:30.720 So this is something that we're gonna have to go grab from the other Vieuxtemps PI. 05:30.730 --> 05:35.790 So if we go ahead and look for this authenticate here, we're gonna go ahead and copy this. 05:36.800 --> 05:42.800 And come back to our views, that pie, we'll paste this in, we don't need the log in or the log out 05:42.830 --> 05:44.210 so we can go ahead and get rid of that. 05:44.210 --> 05:46.430 We just need to authenticate. 05:46.850 --> 05:49.970 And what we're gonna do is authenticate with. 05:50.030 --> 05:54.020 And again, this is another line that we can come grab from this log in. 05:54.590 --> 05:58.460 We're gonna say we want to authenticate with the username and the following password. 05:58.520 --> 06:06.260 So we'll go ahead and copy this and let's come back to our code and will replace that there. 06:06.850 --> 06:07.080 OK. 06:08.120 --> 06:11.960 So make sure that we are now using data instead of the request up post. 06:11.970 --> 06:14.840 Wherever you see, request up post will place that with data. 06:15.380 --> 06:17.150 Also here will replace that. 06:17.180 --> 06:19.670 So someone passes in a username and a password. 06:20.120 --> 06:22.370 We attempt to authenticate that user. 06:22.400 --> 06:25.760 Now this is where we're going to take things a little bit differently here. 06:25.790 --> 06:28.790 So I'm going to keep that Jason response because we're going to use in a second. 06:28.820 --> 06:35.620 But first, let's make sure that the user exists or TSA if user is none. 06:35.810 --> 06:38.900 Meaning that we weren't able to authenticate the user. 06:39.380 --> 06:43.280 Then we need to tell the user things didn't really work out here. 06:43.310 --> 06:46.550 So I'm going to copy the same air that we have above there. 06:46.990 --> 06:50.600 I'm going to pace it down below, except for instead of that user names already taken. 06:51.050 --> 06:56.720 We're going to say something like, I could not log in. 06:57.260 --> 06:58.910 Please check. 06:59.830 --> 07:02.080 Username and password. 07:03.330 --> 07:06.330 OK, so we've got that situation handled. 07:06.360 --> 07:12.550 We need to have the ELT's situation, right, which the Elsas to say, OK, we did get back a user. 07:12.550 --> 07:14.040 So what should we do? 07:14.100 --> 07:19.530 Well, ultimately, what we want to do is return back the user a proper token. 07:19.540 --> 07:20.760 So we're going to say token. 07:21.420 --> 07:24.600 But the question is, how do we go about getting that token? 07:24.600 --> 07:28.220 And in this case, it's not a tool one for a create, it's just a two hundred. 07:29.550 --> 07:32.850 But the question is, how do we get the token object? 07:32.880 --> 07:34.950 Because there's sort of two situations here. 07:35.490 --> 07:40.080 The first one is, is that we'll just be able to find a token that already exists in the database. 07:40.140 --> 07:43.980 So, for example, for next six, there's already a token. 07:44.100 --> 07:50.370 So for next six, all that we have to do is just say, oh, well, the token is equal to the same thing 07:50.370 --> 07:53.460 that we did up here, except it's a get, not a create. 07:53.490 --> 08:00.210 We're gonna say token is equal to objects dot get where users equal to user. 08:00.810 --> 08:01.650 Simple enough, right. 08:01.680 --> 08:03.200 Is just saying go and grab that. 08:03.210 --> 08:10.950 The problem is what happens if Nick not Nick six or you know, Joe, Mark, Sally, whoever it is tries 08:10.950 --> 08:11.580 to log in. 08:12.180 --> 08:13.830 That doesn't already have a token. 08:13.950 --> 08:15.330 Well, we're gonna get an error here. 08:15.660 --> 08:18.220 So what we're gonna do is gonna wrap this. 08:18.240 --> 08:20.790 We're gonna do a try colon tab. 08:20.940 --> 08:28.320 This over there, we're gonna do an except to say, OK, if you don't have one, then this is really 08:28.320 --> 08:33.150 where we copy this line of code directly and just say, go ahead and create a new token. 08:33.240 --> 08:39.840 So whether we pull one from the database or we create a brand new one, we're gonna go ahead and return 08:39.840 --> 08:40.200 this. 08:40.620 --> 08:42.540 So there you have it. 08:42.660 --> 08:44.700 This is how our log in works. 08:44.970 --> 08:46.470 Let's go ahead and test it out here. 08:46.470 --> 08:52.380 So I'm going to go ahead and save what we've got here and we're gonna come back to our terminal. 08:52.440 --> 08:54.240 Now we've got to do a test. 08:54.270 --> 08:56.670 Now, this is gonna be a little bit of Kerl work. 08:56.700 --> 09:02.390 So let's go ahead and copy what we had for an existing post here and let's sort of work on this. 09:02.400 --> 09:05.710 So instead of doing sign up, we want to do log in. 09:05.730 --> 09:10.560 We still want the application slash Jason and we want to do the user name and password. 09:10.800 --> 09:11.010 Yep. 09:11.040 --> 09:12.010 This all looks good. 09:12.110 --> 09:15.270 Okay, so I'm gonna copy this line of code here. 09:15.350 --> 09:15.650 OK. 09:16.350 --> 09:17.790 And we'll see. 09:17.850 --> 09:20.570 This should be working for Nick one, right. 09:20.580 --> 09:22.830 Since he does not have a token. 09:22.860 --> 09:24.180 It should be generating one. 09:24.540 --> 09:26.640 So let's go ahead and go to the terminal here. 09:27.150 --> 09:28.290 I'm going to paste this in. 09:28.710 --> 09:30.000 Hit enter and look at that. 09:30.450 --> 09:35.490 Nick one gets a token and we should be able to if we do this, go ahead and hit up. 09:36.060 --> 09:38.370 Nick six, for instance. 09:39.110 --> 09:39.410 OK. 09:39.600 --> 09:40.020 And look at that. 09:40.050 --> 09:43.200 That's the exact same token as was there before. 09:43.200 --> 09:45.720 And let's go ahead and log in with the original Nick. 09:47.240 --> 09:47.600 OK. 09:48.320 --> 09:49.070 Just like that. 09:49.310 --> 09:50.840 Oh, look, we got to talk it. 09:50.870 --> 09:51.590 So this is so cool. 09:51.620 --> 09:56.380 I want to I want to see if I can get Nick's current outstanding to do so. 09:56.420 --> 09:59.660 Let's go ahead and move back a little bit here. 10:00.740 --> 10:01.310 Right, Kay? 10:01.480 --> 10:07.270 So I want to go look at the tattoos, I'm going to delete the previous token and let's pass in the token 10:07.270 --> 10:08.690 for our NIC user. 10:08.920 --> 10:12.520 And look at that ad in API to my site. 10:14.750 --> 10:18.860 We've done it so to fully come, you know, all the way full circle here. 10:18.890 --> 10:22.340 I'm going to copy what we've what we have here. 10:22.980 --> 10:23.250 OK. 10:23.480 --> 10:25.490 Well, at least next token here. 10:26.300 --> 10:27.920 Let's go ahead and copy that. 10:29.570 --> 10:31.080 In a pace this now. 10:31.300 --> 10:34.750 In fact, let's go ahead and copy the whole thing here, we're gonna need this to create the monster 10:34.750 --> 10:35.070 curl. 10:35.290 --> 10:39.130 I'm going to attempt to vía our API complete. 10:39.250 --> 10:40.630 The very last to do that. 10:40.630 --> 10:42.100 We have that to do of one K. 10:42.750 --> 10:44.470 So let's go ahead and paces here. 10:44.920 --> 10:46.550 So it's going to be a post. 10:46.570 --> 10:54.880 Remember, we want to do a post and we want to go to API slash to dos slash and then give the I.D. which 10:54.880 --> 10:57.460 it's that last one has an idea of one. 10:57.610 --> 10:57.880 Right. 10:57.940 --> 10:59.480 Add an API to my Web site. 11:00.040 --> 11:01.210 So we're gonna do that. 11:01.270 --> 11:02.800 And we want to complete it. 11:03.370 --> 11:07.600 Now remember, what's great about this is we don't have to pass in any sort of data. 11:07.630 --> 11:11.290 So in fact, we can go ahead and get rid of all of this. 11:11.680 --> 11:12.020 Okay. 11:12.550 --> 11:15.040 All we need to do is provide that. 11:15.040 --> 11:17.630 We want the authorization for this particular token. 11:17.630 --> 11:20.140 So I'm going to copy and paste that there. 11:20.620 --> 11:24.430 So now if we take this entire line here, copy this. 11:25.520 --> 11:29.210 Paste it in to the terminal hit enter. 11:32.020 --> 11:33.200 Post is now. 11:33.290 --> 11:34.510 Oh, it's not a post. 11:34.540 --> 11:36.550 It's a put or a patch. 11:37.330 --> 11:37.880 So let's go. 11:37.980 --> 11:39.820 Well, I'm gonna it's gonna take forever. 11:39.820 --> 11:40.550 I would do it that way. 11:40.570 --> 11:41.560 Let's just do this. 11:41.920 --> 11:43.360 Let's change this to a put. 11:44.220 --> 11:44.650 Okay. 11:44.800 --> 11:46.350 And we will copy this line. 11:46.360 --> 11:50.530 Come back to the terminal paste in and look at that. 11:50.560 --> 11:52.820 If we go ahead and go back, let's do the. 11:52.840 --> 11:53.500 Get here. 11:53.770 --> 11:56.110 We'll get an empty list. 11:56.350 --> 11:58.240 There's no more to do. 11:58.270 --> 11:59.410 Items to be completed. 11:59.710 --> 12:03.790 And if we really want to see what this looks like here, let's go back to our to do site. 12:03.820 --> 12:04.120 Right. 12:04.360 --> 12:08.710 So let's log in the via the original nick. 12:09.610 --> 12:12.100 And let's do the password here. 12:13.780 --> 12:14.830 Let's look at the completed. 12:15.670 --> 12:16.540 We've done it. 12:16.960 --> 12:20.170 We have added an API to our Web site. 12:20.290 --> 12:21.700 How good does that feel? 12:21.730 --> 12:24.420 You've got to be do with us feeling great right now. 12:24.470 --> 12:25.600 Well, I'm just excited. 12:25.630 --> 12:26.140 Think about it. 12:26.250 --> 12:27.730 OK, let's go ahead. 12:27.820 --> 12:28.870 Wrap everything up. 12:29.140 --> 12:29.790 I'll see you soon.