Flask Rest API - How to use Bearer API token in python requests Flask Rest API - How to use Bearer API token in python requests flask flask

Flask Rest API - How to use Bearer API token in python requests


If you want us to use Bearer tokens take a look at Miguel Grinberg's Application Programming Interfaces and scroll down to the "Tokens in the User Model". However, the whole thing deserves a read.

Another article is Real Pythons's Token-Based Authentication with Flask.

Both of these will help with understanding and implementation of bearer tokens.


curl -u uses not bearer tokens but BasicAuth (via login and password). Try this:

url = 'http://%s:%s@127.0.0.1:5000/api/resource' % (    'eyJhbGciOiJIUzI1NiIsImlhsfsdfsdzNCwiZXhwIjoxNTMwNzksdfsdsdRF.eyJpZCI6MX0.YhZvjKiafmv-qrvAxVo7UKQuohS2vkF-9scpuqsKRuw',    'sp',)headers = {    'Content-Type': 'application/json',}response = requests.request("GET", url, headers=headers)

But the recommended way is passing login and password encoded in header:

import base64url = 'http://127.0.0.1:5000/api/resource'headers = {    'Content-Type': 'application/json',    'Authorization': 'Basic %s' % base64.b64encode('%s:%s' % (            'eyJhbGciOiJIUzI1NiIsImlhsfsdfsdzNCwiZXhwIjoxNTMwNzksdfsdsdRF.eyJpZCI6MX0.YhZvjKiafmv-qrvAxVo7UKQuohS2vkF-9scpuqsKRuw',            'sp',        ),    ),}response = requests.request("GET", url, headers=headers)