pytest-api/apiclient/oauth_helper.py

30 lines
809 B
Python
Raw Permalink Normal View History

2024-07-22 18:26:27 +00:00
import sys
2024-07-20 10:47:11 +00:00
from oauthlib.oauth2 import LegacyApplicationClient, OAuth2Error
from requests_oauthlib import OAuth2Session
2024-07-22 18:26:27 +00:00
2024-07-20 10:47:11 +00:00
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)
2024-07-22 18:26:27 +00:00
sys.exit(e.status_code)
2024-07-20 10:47:11 +00:00
return token