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


Talk Python's Mastodon Michael Kennedy's Mastodon