def build_oauth2(username, password, token_url, client_id): """Creates an OAuth2 object usable by requests.get()""" client = oauthlib.oauth2.LegacyApplicationClient(client_id=client_id) token = requests_oauthlib.OAuth2Session(client=client).fetch_token( token_url=token_url, username=username, password=password, client_id=client_id, ) return requests_oauthlib.OAuth2(client_id=client_id, client=client, token=token)
def signed_session(self, session=None): """Create requests session with any required auth headers applied. If a session object is provided, configure it directly. Otherwise, create a new session and return it. :param session: The session to configure for authentication :type session: requests.Session :rtype: requests.Session """ session = session or requests.Session( ) # Don't call super on purpose, let's "auth" manage the headers. session.auth = oauth.OAuth2(self.id, token=self.token) return session