Home > database >  Calling API through requests Library to get access token
Calling API through requests Library to get access token

Time:02-24

I am trying to get the access token for a specific API, the curl command is given below:

    curl --location --request POST 'https://example.com/oauth/connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=abcd' \
--data-urlencode 'client_secret=abc' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=us' \
--data-urlencode 'scope=a' \
--data-urlencode 'password=abc'

I have the following code in python:

url = 'https://example.com/oauth/connect/token'
body = {'client_id':'abcd', 
'client_password':'abc', 
'grant_type':'password',
'username':'us',  
'scope':'a',
'password':'abc'}
headers = {'Content-Type':'application/x-www-form-urlencoded'}

response = requests.post(url, data=body, headers=headers, verify=False)
print(response,response.content)

I keep receiving a <Response [400]> b'{"error":"invalid_client"}' error.

I believe I have tried almost everything there is out there and I am lost. Please let me know if I am missing something really simple.

CodePudding user response:

I noticed, that your curl command uses client_secret, while your requests command uses client_password.

For debugging such requests, I recommend free pages like https://www.toptal.com/developers/postbin/, where you can send the curl and the requests message and compare what arrived on the endpoint.

  • Related