pytest-api/apiclient/oauth_helper.py

28 lines
801 B
Python
Raw Normal View History

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