<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic OAuth Client 400 Error in Open Q&amp;A</title>
    <link>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284246#M37374</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am trying to use an OAuth client in Python&lt;/P&gt;
&lt;P&gt;The following cURL command works:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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.&amp;lt;ID&amp;gt;' --data-urlencode 'client_secret=dt0s02.&amp;lt;SECRET&amp;gt;' --data-urlencode 'resource=urn:dtaccount:&amp;lt;ACCOUNT&amp;gt;' -
-data-urlencode 'scope=&amp;lt;POLICIES SPACE SEPARATED&amp;gt;'&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;However, the following Python POST request gives me a 400 error.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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)&lt;/LI-CODE&gt;
&lt;P&gt;Variables are all defined correctly same as values used in cURL command.&lt;/P&gt;
&lt;P&gt;Can anyone see where I'm going wrong?&lt;BR /&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Aug 2025 06:32:23 GMT</pubDate>
    <dc:creator>lmcnicol</dc:creator>
    <dc:date>2025-08-21T06:32:23Z</dc:date>
    <item>
      <title>OAuth Client 400 Error</title>
      <link>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284246#M37374</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am trying to use an OAuth client in Python&lt;/P&gt;
&lt;P&gt;The following cURL command works:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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.&amp;lt;ID&amp;gt;' --data-urlencode 'client_secret=dt0s02.&amp;lt;SECRET&amp;gt;' --data-urlencode 'resource=urn:dtaccount:&amp;lt;ACCOUNT&amp;gt;' -
-data-urlencode 'scope=&amp;lt;POLICIES SPACE SEPARATED&amp;gt;'&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;However, the following Python POST request gives me a 400 error.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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)&lt;/LI-CODE&gt;
&lt;P&gt;Variables are all defined correctly same as values used in cURL command.&lt;/P&gt;
&lt;P&gt;Can anyone see where I'm going wrong?&lt;BR /&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 06:32:23 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284246#M37374</guid>
      <dc:creator>lmcnicol</dc:creator>
      <dc:date>2025-08-21T06:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth Client 400 Error</title>
      <link>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284247#M37375</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 06:56:37 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284247#M37375</guid>
      <dc:creator>Julius_Loman</dc:creator>
      <dc:date>2025-08-20T06:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth Client 400 Error</title>
      <link>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284252#M37377</link>
      <description>&lt;P&gt;Turns out my ENV variable for the scope had an extra space hiding in the middle of one of the scopes&lt;BR /&gt;Feel very silly now&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_squinting_face:"&gt;😆&lt;/span&gt;&lt;BR /&gt;Thanks for your response, yes I've also removed the unnecessary encoding.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 07:04:16 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Open-Q-A/OAuth-Client-400-Error/m-p/284252#M37377</guid>
      <dc:creator>lmcnicol</dc:creator>
      <dc:date>2025-08-20T07:04:16Z</dc:date>
    </item>
  </channel>
</rss>

