add audience and scope

This commit is contained in:
Greg Gauthier 2024-07-19 18:47:03 +01:00
parent 83fed3f7c4
commit 56c3fe6cfc

View File

@ -2,11 +2,18 @@ from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient from oauthlib.oauth2 import BackendApplicationClient
def get_token(client_id, client_secret, token_url, username, password): def get_token(client_id, client_secret, token_url, username, password, audience, scope):
client = BackendApplicationClient(client_id=client_id) client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client) oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, username=username, password=password, client_id=client_id, token = oauth.fetch_token(
client_secret=client_secret) token_url=token_url,
username=username,
password=password,
client_id=client_id,
client_secret=client_secret,
audience=audience,
scope=scope
)
return token return token
@ -21,9 +28,13 @@ client_secret = '<your_secret_id>'
token_url = '<oauth_token_url>' token_url = '<oauth_token_url>'
username = '<your_username>' username = '<your_username>'
password = '<your_password>' password = '<your_password>'
audience = '<intended_audience>'
scope = ['scope1', 'scope2']
# Fetch token # Fetch token
token = get_token(client_id, client_secret, token_url, username, password) token = get_token(client_id, client_secret, token_url, username, password, audience, scope)
# Now we can make API calls # Now we can make API calls
api_response = api_client('https://api.example.com/resource', token) api_response = api_client('https://api.example.com/resource', token)
print(api_response)