2

Trying to get the access token using Oauth2 password credentials, also I tried to change the grant_type value by client_credentials still received the same error. Tried code:

import requests
payload = f"grant_type=password_credentials&client_id={CI}&client_secret={CS}&username={UN}&password={PW}"
headers = { 'accept': "application/json", Config: Live }
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

Error:

{"error_description":"grant_type is required","error":"invalid_request"}

Any idea what wrong I am doing?

Additionally, I do have one more field access token but dont know where I should place.

Update in previous code:

headers = {'Authorization':  AccessToken, 'accept': "application/json", Config: Live }
response = requests.request("POST", url, data=payload, headers=headers)
  • just a suggestion, makybe replicate api usage using postman and then implement them in python – sahasrara62 Oct 06 '22 at 08:59
  • @sahasrara62 thank you for your reply, actually I am getting perfact response with postman. But the issue I new postman getting following two issues https://stackoverflow.com/questions/73971058/set-up-get-new-access-token-with-oauth2-credentials-using-python and https://stackoverflow.com/questions/73963082/sync-token-in-postman – user20042770 Oct 06 '22 at 09:04
  • just in this case, from postman, take curl command thing, and from that recreate those post request, and parameter in python, and see if you getting same thing as of postman, if yes then there you go, you have made a prototype – sahasrara62 Oct 06 '22 at 09:07
  • Hi, will your solution help to overcome this issue https://stackoverflow.com/questions/73963082/sync-token-in-postman ? – user20042770 Oct 06 '22 at 09:16
  • my solution is just an approach for you to help how oauth works – sahasrara62 Oct 06 '22 at 11:08

1 Answers1

2

if I am not wrong, after second header update your code is fine. Just need to add one more parameter: Content-Type

{"Content-Type":"application/x-www-form-urlencoded"}

Onemore thing, Grant Type should be password instead of password_credentials.

Reference:

https://discuss.python.org/t/urlencoded-sending-a-json-string/13795

https://gist.github.com/wadewegner/7557434

R. Baraiya
  • 1,490
  • 1
  • 4
  • 17