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