Consuming HTTP Services in Python Transcripts
Chapter: Accessing authenticated HTTP services
Lecture: Making authenticated requests with requests

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's add the ability to access the restricted blog service with requests. So you can see up here that I have updated the base url,
0:11 I've actually moved this section of the url, up into the top so I tweaked the code just slightly,
0:16 but you can see most importantly this is the restricted version, so if we come down here to this part where we get post, this should look familiar,
0:22 I want to come over here and say we are looking for application json we are going to do a get just against that url, and let's see what we get here,
0:29 so try a list, it says no, no 401 you must authenticate with basic auth,
0:34 okay, so I am going to show you this in request and it is going to seem super easy,
0:39 and then when we go look at this at the builtins, you are going to be like oh my gosh, why do they do it that way; so, remember
0:45 we had our username is kennedy and the password was super_lockdown, alright, and then, all we have to do is come over here
0:55 and say auth=(user, password) okay, and let's tell PyCharm that lockdown is actually a word,
1:06 okay, let's run this again, and we'll try our list, boom, ta- da, it's working,
1:09 of course, we can't delete anything so if I try to delete one, it says 401 you must authenticate, okay, so this is really all that is required,
1:17 and maybe we would even move this somewhere else, just in case it changes,
1:21 I could put this up here as global, I could write a function that is going to return it,
1:25 something to that effect, but let me just copy this over, and we will put in the other one,
1:31 so here when we are doing a post, again, we just say auth is this, when we are doing a put, we just say the auth is this, and last but not least,
1:42 when we're doing a delete, we are going to say this, alright, let's try to list again,
1:46 okay, that works, let's try to add one, we call this doomed post, I am going to try to delete it, okay so perfect, that woks,
1:56 let's see yeah, doomed post, let's try to update it, I am just going to increment the count so number 2, same, same, view count 1999, try to list it,
2:06 it was updated, now let's delete it, alright, which one is 2, goodbye 2, 2 is gone.
2:10 Okay, so authentication with requests, super easy, auth username:password,
2:16 but remember, make sure this url is something that is going to keep your data safe, either something internal you trust, or something over ssl.


Talk Python's Mastodon Michael Kennedy's Mastodon