Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up Get New Access Token with OAuth2 credentials using python

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)
like image 957
user20042770 Avatar asked Sep 15 '26 13:09

user20042770


1 Answers

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

like image 88
R. Baraiya Avatar answered Sep 17 '26 03:09

R. Baraiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!