Sep-27-2021, 04:46 AM
(This post was last modified: Sep-27-2021, 04:46 AM by herobpv.
Edit Reason: typo
)
Modified code:
Error:
import sys import requests import json import logging import time logging.captureWarnings(True) api_url = "https://webapi.com/api/v1/data" def get_new_token(): acc_token_url = "https://webapi.com/connect/accesstoken" 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)Below is the new error message I get after modifying the code.
Error:
Error:requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.webapi.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))