python requests authorization bearer token

Python Requests Authorization Bearer Token

As a web developer, I have come across the use of Bearer Token quite often. It is a type of token authentication that is widely used in API authentication. In this PAA, I will be discussing how to make requests with Python requests library and how to add a Bearer token authorization header to it.

Using Python Requests Library

Python requests library is a popular library used for making HTTP requests. It is a simple yet powerful tool that makes it easy to interact with APIs. To make a request with requests library, you need to install it via pip.


pip install requests

Once you have installed the requests library, you can start making requests. In the following example, we will be making a GET request to a sample API endpoint:


import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts')

print(response.text)

This will make a GET request to the specified endpoint and print the response text. However, some APIs require authentication before they can be accessed. In such cases, you need to add an authorization header to your request.

Adding Bearer Token Authorization Header

Bearer Token is a type of token authentication that requires the use of a token for authentication. The token is usually provided by the API provider and it is specific to the user or application that is making the request. To add a Bearer token authorization header to your request, you need to use the requests library's headers parameter.

In the following example, we will be making a GET request to a sample API endpoint and adding a Bearer token authorization header:


import requests

url = 'https://api.example.com/data'

headers = {
    'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
}

response = requests.get(url, headers=headers)

print(response.json())

In this example, we are creating a headers dictionary and adding a Bearer token authorization header to it. We then pass this dictionary as the headers parameter in our GET request. The API endpoint will then authenticate the request based on the Bearer token provided.

As you can see, adding a Bearer token authorization header to a Python requests library is quite simple. All you need to do is create a headers dictionary and add the authorization header to it.

Using Environment Variables

It is not recommended to hardcode the access token or any sensitive information in your code. One way to avoid this is by using environment variables. You can store your access token as an environment variable and use it in your code.

In the following example, we will be using the os library to access environment variables:


import requests
import os

url = 'https://api.example.com/data'

headers = {
    'Authorization': f'Bearer {os.environ.get("ACCESS_TOKEN")}'
}

response = requests.get(url, headers=headers)

print(response.json())

In this example, we are using the os library's environ.get() method to access the ACCESS_TOKEN environment variable. We then use an f-string to add the token to the authorization header.

As you can see, using environment variables is a more secure way of accessing sensitive information like access tokens.