Sep-27-2021, 06:54 PM
(Sep-27-2021, 05:04 AM)snippsat Wrote: Requests has own library for OAuth 2 in Doc.
requests-oauthlib.
Thank you for your response. I have modified my code accordingly. Please let me know if I am doing it right.
import sys import requests from requests_oauthlib import OAuth2Session from oauthlib.oauth2 import BackendApplicationClient import json import logging import time logging.captureWarnings(True) api_url = "https://webapi.com/api/v1/data" #Obtain credentials from OAuth provider client_id = 'client' client_secret = 'secret' #Fetch an access token from the provider. client = BackendApplicationClient(client_id=client_id) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url='https://webapi.com/connect/accesstoken', client_id=client_id, client_secret=client_secret) #Automatic token refresh and update client = OAuth2Session(client_id, token=token) response = client.get(api_url) data = response.json()Regards