Hi, I am new to python programming and trying to make a RESTful api call and retrieve json payload from a GET method. I am using the below code but I get error and not sure how to proceed from here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import sys import requests import json import logging import time logging.captureWarnings( True ) def get_new_token(): client_id = 'client' client_secret = 'secret' token_req_payload = { 'grant_type' : 'client_credentials' } token_response = requests.post(acc_token_url , data = token_req_payload, verify = False , allow_redirects = False , auth = (client_id, client_secret)) if token_response.status_code ! = 200 : print ( "Failed to obtain token from OAuth2 server" , file = sys.stderr) sys.exit( 1 ) print ( "Successfuly obtained a new token from OAuth2 server" ) tokens = json.loads(token_response.text) return tokens[ 'access_token' ] token = get_new_token() while True : api_call_headers = { 'Authorization' : 'Bearer ' + token} api_call_response = requests.get(api_url, headers = api_call_headers, verify = False ) if api_call_response.status_code = = 401 : token = get_new_token() else : print (api_call_response.text) |
Error:Traceback (most recent call last):
NameError: name 'token_response' is not defined