Basic Authentication in HTTP Requests

Many web services, such as APIs, require authentication. This can often be a daunting topic for beginner or novice programmers, alike. This is especially true, given that there are many different types of authentication. Thankfully, the requests library comes with a large number of different authentication methods built-in, making the process simple and easy.

But lets face it, sometimes you just want to get sh*t done without all the fancy bells and whistles. That’s where basic authentication comes in! Basic authentication is like the little brother of OAuth2 or OpenID Connect it’s not as cool, but it gets the job done.

Lets see how we can pass in a username and password into a simple GET request using the HTTPBasicAuth class provided by the requests library:

# Import the requests library to make HTTP requests
import requests

# Import the HTTPBasicAuth class from the requests library to handle basic authentication
from requests.auth import HTTPBasicAuth

# Create an instance of the HTTPBasicAuth class with the username and password provided
auth = HTTPBasicAuth('user', 'pass')

# Make a GET request to the specified URL with the provided authentication
response = requests.get('https://httpbin.org/basic-auth/user/pass', auth=auth)

# Print the response from the request
print(response)

# Output:
# <Response [200]>

# The response code 200 indicates that the request was successful

# The response object contains information about the request, such as the response code, headers, and content

# The auth parameter in the get() method specifies the authentication to be used for the request

# The HTTPBasicAuth class takes in the username and password as parameters and creates a Basic Authentication header for the request

# This header is necessary for the server to verify the user's credentials and allow access to the requested resource

That’s it! You can replace the username and password with your own credentials, and you’re good to go. No need for fancy tokens or OAuth2 dance routines just a simple tuple of your login info.

But wait, theres more! If you don’t want to create an HTTPBasicAuth object every time, the requests library makes it even easier by allowing you to pass in a tuple containing your username and password into the auth= parameter:

# Import the requests library
import requests

# Use the get method from the requests library to make a GET request to the specified URL
# and print the response
print(requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass')))

# The auth parameter allows us to pass in a tuple containing our username and password
# to create an HTTPBasicAuth object for authentication

# The requests library makes it easy to create an HTTPBasicAuth object by allowing us to
# pass in a tuple of our login information directly into the auth parameter

# The get method returns a response object, which we can then print to see the response from the server

See, I told you it was easy! No need to create a new object every time just pass in your credentials as a tuple and let the requests library handle the rest for you.

In the following section, well learn how to use digest authentication with Python Requests. But first, let’s grab some coffee and enjoy this beautiful sunset while we wait for our next tutorial!

SICORPS