simple oath2 api client

This commit is contained in:
Greg Gauthier 2024-07-19 18:40:09 +01:00
parent ea4794a7bc
commit 83fed3f7c4
3 changed files with 32 additions and 0 deletions

0
api-client/__init__.py Normal file
View File

29
api-client/eg.py Normal file
View File

@ -0,0 +1,29 @@
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
def get_token(client_id, client_secret, token_url, username, password):
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, username=username, password=password, client_id=client_id,
client_secret=client_secret)
return token
def api_client(base_url, token):
client = OAuth2Session(token=token)
response = client.get(base_url)
return response.content
client_id = '<your_client_id>'
client_secret = '<your_secret_id>'
token_url = '<oauth_token_url>'
username = '<your_username>'
password = '<your_password>'
# Fetch token
token = get_token(client_id, client_secret, token_url, username, password)
# Now we can make API calls
api_response = api_client('https://api.example.com/resource', token)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
oauthlib~=3.2.2
requests~=2.32.3
requests-oauthlib~=2.0.0