Consuming HTTP Services in Python Transcripts
Chapter: Accessing authenticated HTTP services
Lecture: Authenticated requests with Python 3's urllib.requests

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now let's look at how we add authentication to the builtin version
0:04 of the http library which is of course urllib.
0:08 So let's go over here and run this new version Python 3
0:12 and let's try to do a list again, unauthorized, not the best user experience, was it.
0:18 Ok, so I guess we should probably add some try except handling
0:22 around all those things, but, we are just going to add the username and password for now.
0:27 You can see at the top we've got the username and password set here,
0:30 and on request, we set this every time we created a request
0:33 we would set the username and password, but this time, what we are going to do instead
0:36 is we are going to register some global authentication for our app,
0:41 so if you log in to this url, always use this username and this authentication scheme,
0:45 okay, so we'll have a register auth, something like that,
0:50 we are going to call that at the beginning, so what we are going to do is
0:53 notice, up here, importing urllib.request and in urllib.request
0:59 we can get a thing called a password_manager, notice,
1:04 here we have a http password manager one with prior auth, one with default realm,
1:09 all of those things okay, so we are going to use this one,
1:12 we are going to create this thing,
1:15 we are going to say password_manager.add_password, then it wants the realm,
1:19 this is like the domain sort of thing so we'll say none
1:21 and then we're going to give it the base url
1:24 and then we just give it the user and the password, okay,
1:28 and then we need to whenever we need to work with this,
1:30 we are going to create a handler that is based on this,
1:33 okay, so up here, let's have an authenticated handler,
1:45 and we are going to set that here, we'll say urllib.request.http basic auth handler,
1:53 and and it's going to take a password manager,
1:56 okay, you can see this is totally obvious, right,
1:59 like you wouldn't want to just pass this informational log,
2:01 okay, and then we can build an opener, right,
2:05 so we'll say opener=urllib.request.build_opener,
2:11 I want to give it the authenticated handler,
2:13 actually I don't think we need to store that globally,
2:16 okay, and then we'll say urllib.requests.install opener,
2:22 okay, so we have a password manager, we set the password and the url
2:26 that we are going to authenticate for, so whenever it sees that url
2:30 it's going to use that password, we want to create the authenticated handler
2:36 for that password manager, create an authenticated opener and install this globally,
2:39 so now, when we go and run this, let's see what happens,
2:42 let's try to list it, oh, authenticated,
2:45 now of course, just to show you that it had some effect,
2:49 if we don't do this, you saw that 401 unauthorized, okay,
2:51 so this is super non obvious, one of the benefits is you can register this globally,
2:56 and then just forget about it, but I don't know, that's super clumsy to me
3:01 but there you have it, so let's try to add another one,
3:04 I'll call this Py3 builtin doomed post because we are going to ultimately delete it,
3:09 and put some content, it has one view, great, listed again, there is number 2,
3:14 it's going to update it, it's got a few more views,
3:18 see authentication is totally working, finally, let's try to delete it,
3:21 we want to delete number 2, and is number 2 gone- it's gone.
3:25 Well, there is a new number 2, how about that, alright,
3:27 so that is authentication with the Python builtins.