Python Forum
Python requests oauth2 access token
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python requests oauth2 access token
#5
Modified code:

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'))
Reply


Messages In This Thread
Python requests oauth2 access token - by herobpv - Sep-20-2021, 02:39 AM
RE: Python requests oauth2 access token - by herobpv - Sep-27-2021, 04:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python requests library .JSON() error mHosseinDS86 6 3,591 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Refresh token for Wyze SDK duckredbeard 0 1,139 May-16-2022, 04:33 AM
Last Post: duckredbeard
  Need to sign JWT token with JWK key stucoder 1 1,732 Feb-21-2022, 09:04 AM
Last Post: stucoder
  unexpected token < in json at position 0 Frodoxzibit 5 2,887 Jul-10-2021, 09:33 AM
Last Post: Larz60+
  Python Requests SSL Aussie 0 2,019 Jan-07-2021, 02:09 AM
Last Post: Aussie
  Python Requests Aussie 2 2,804 Dec-23-2020, 03:24 AM
Last Post: Aussie
  Python Requests package: Handling xml response soumyarani 1 2,216 Sep-14-2020, 11:41 AM
Last Post: buran
  Python API and requests deep_logic 9 4,419 Jul-29-2020, 03:47 PM
Last Post: ndc85430
  Can't access the net using Python's Selenium module ShishirModi 2 2,103 Jul-21-2020, 06:03 AM
Last Post: ShishirModi
  How to save Python Requests data sent to server? RedLeonard 5 5,149 Jul-05-2020, 10:33 AM
Last Post: RedLeonard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020