Skip to content

Requesting An OAuth Token

Requesting an OAuth token for API calls#

This is specifically for the OAuth 2.0 client credentials flow and resource owner password flow.

If you request an OAuth token and receive this error:

{'error': 'unsupported_grant_type'}

Yet you are sending password as the auth type?

response = requests.post(
    'https://{base_url}/token',
    json={'grant_type': 'password', 'username': 'my_user', 'password': 'my_password'},
    headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
    verify=False
)

The problem is that the OAuth spec expects application/x-www-form-urlencoded as the content type of the token request.

The fix is:

  • from using json to application/x-www-form-urlencoded
  • use data=.. instead of json=... for the requests library
response = requests.post(
    'https://{base_url}/token',
    data={'grant_type': 'password', 'username': 'my_user', 'password': 'my_password'},
    headers={'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'},
    verify=False
)

Sources#