Skip to content

Using easyauth with FastAPI and requests #75

Answered by codemation
w-markus asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @w-markus , in order to access a protected endpoint, a script will need to attach an auth header containing a generated bearer token.

import os
import requests

username = 'admin' # or other created user
password = os.environ['PW_FROM_ENV']

# required header for using username / password to request a token
headers = {'accept': 'application/json', 'content-type': 'application/x-www-form-urlencoded'}

token_request = requests.post(
    'http://my-token_server/auth/token', 
    headers=headers, 
    data=f'username={username}&password={password}'
)

token = token_request['access_token'] # you could use service account token instead
headers = {'Authorization': f'Bearer {token}'}

# use h…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@w-markus
Comment options

@w-markus
Comment options

Answer selected by w-markus
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants