Home > front end >  Set up Get New Access Token with OAuth2 credentials using python
Set up Get New Access Token with OAuth2 credentials using python

Time:10-07

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)

CodePudding user response:

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

  • Related