python-snippets/api-client/oauth_helper.py
2024-07-19 23:45:11 +01:00

28 lines
794 B
Python

from oauthlib.oauth2 import LegacyApplicationClient, OAuth2Error
from requests_oauthlib import OAuth2Session
from config import get_cfg
import sys
def get_legacy_token(env):
cfg = get_cfg(env)
client = LegacyApplicationClient(client_id=cfg['client_id'])
oauth = OAuth2Session(client=client)
try:
token = oauth.fetch_token(
token_url=cfg['token_url'],
username=cfg['username'],
password=cfg['password'],
client_id=cfg['client_id'],
client_secret=cfg['client_secret'],
audience=cfg['audience'],
scope=cfg['scopes']
)
except OAuth2Error as e:
print("OAuth2 Error: ", str(e), file=sys.stderr)
exit(e.status_code)
return token