To make a request with a Bearer token using the Python requests library, you need to include the token in the request headers. Here’s an example of how to do it:
Example: python requests bearer token
main.py
import requests url = 'https://api.example.com/endpoint' websiteData = dict( name="Yttags", job="Free Online Tools", ) authToken = "your_bearer_token_here" headers = { 'Authorization': 'Bearer ' + authToken, 'Content-Type': 'application/json' } response = requests.post(url, websiteData, headers) data = response.json() print(data)
Example : Get Request
import requests # Your Bearer token bearer_token = "your_bearer_token_here" # URL to make the request to url = "https://api.example.com/endpoint" # Set the Authorization header with the Bearer token headers = { "Authorization": f"Bearer {bearer_token}", "Content-Type": "application/json" # Adjust content type as needed } # Make the request response = requests.get(url, headers=headers) # Check the response status code if response.status_code == 200: # Request was successful data = response.json() print(data) else: # Request failed print("Request failed with status code:", response.status_code)
Replace “your_bearer_token_here” with your actual bearer token, and “https://api.example.com/endpoint” with the URL of the API endpoint you want to access.
In this example, the requests.get() function is used to make a GET request to the specified URL. The Bearer token is included in the request headers using the “Authorization” key. The response object contains the server’s response, which you can then process as needed.