# OAuth Client 400 Error

**URL:** <https://community.dynatrace.com/t/oauth-client-400-error/53032>\
**Category:** Open Q&A\
**Tags:** user-management, python, dynatrace-api\
**Created:** [August 20, 2025, 6:46am UTC](https://community.dynatrace.com/t/oauth-client-400-error/53032 "2025-08-20T06:46:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmcnicol](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@lmcnicol](https://community.dynatrace.com/u/lmcnicol)\
**Post date:** [August 20, 2025, 6:46am UTC](https://community.dynatrace.com/t/oauth-client-400-error/53032/1 "2025-08-20T06:46:03Z")

</div>

Hi,

I am trying to use an OAuth client in Python

The following cURL command works:

```markup
curl --location --request POST 'https://sso.dynatrace.com/sso/oauth2/token' --
header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'grant_type=client_credentials' --data-ur
lencode 'client_id=dt0s02.<ID>' --data-urlencode 'client_secret=dt0s02.<SECRET>' --data-urlencode 'resource=urn:dtaccount:<ACCOUNT>' -
-data-urlencode 'scope=<POLICIES SPACE SEPARATED>'

```

However, the following Python POST request gives me a 400 error.

```markup
headers = {
    "Content-Type": "application/x-www-form-urlencoded"
}

data = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": scope,
    "resource": resource
}

encoded_data = urlencode(data)

print(encoded_data)

bearer_token = requests.post(oauth_api_url, data=encoded_data, headers=headers, verify=False)
print(bearer_token)

```

Variables are all defined correctly same as values used in cURL command.

Can anyone see where I’m going wrong?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [August 20, 2025, 6:56am UTC](https://community.dynatrace.com/t/oauth-client-400-error/53032/2 "2025-08-20T06:56:37Z")

</div>

Probably you should not urlencode the data, but just send it. The HTTP/400 is common when the policies are incorrectly specified, which might be a result of the URL encoding when it is not needed.

---

<div class="post-metadata">

**Author:** ![lmcnicol](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@lmcnicol](https://community.dynatrace.com/u/lmcnicol)\
**Post date:** [August 20, 2025, 7:04am UTC](https://community.dynatrace.com/t/oauth-client-400-error/53032/3 "2025-08-20T07:04:16Z")

</div>

Turns out my ENV variable for the scope had an extra space hiding in the middle of one of the scopes  
Feel very silly now&nbsp;:laughing:  
Thanks for your response, yes I’ve also removed the unnecessary encoding.
