pytest-api/apiclient/oauth_helper.py
Greg Gauthier a727c58cf3
All checks were successful
Pylint / build (3.12) (push) Successful in 9s
execute / build (3.12) (push) Successful in 25s
code cleanup for pylint
2024-07-22 19:26:27 +01:00

30 lines
809 B
Python

import sys
from oauthlib.oauth2 import LegacyApplicationClient, OAuth2Error
from requests_oauthlib import OAuth2Session
from apiclient.config import get_cfg
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['login'],
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)
sys.exit(e.status_code)
return token