# OAuth 2.0 generate token

**URL:** <https://forum.inductiveautomation.com/t/oauth-2-0-generate-token/77809>\
**Category:** Ignition\
**Created:** [August 14, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/oauth-2-0-generate-token/77809 "2023-08-14T14:37:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajay\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Ajay\_Gupta](https://forum.inductiveautomation.com/u/Ajay_Gupta)\
**Post date:** [August 14, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/oauth-2-0-generate-token/77809/1 "2023-08-14T14:37:57Z")

</div>

Hi,

I am trying to **call an endpoint which generates bearer token** which will be used for calling another endpoint for **OAuth 2.0 authentication using the bearer token generated** from the first endpoint.  
The generate token api call works in postman but when i use system.net.httppost method, pass the necessary parameters, i get http error 400.  
Below is the code I am using-

# Generate Bearer Token

```
authServerURL = "MyURLToGenerateBearerToken"
clientId = "MyClientId"
clientSecret = "MyClientSecret"
payload = {"grant_type": "client_credentials","client_id" : clientId,"client_secret":clientSecret}
headerValues = {"Content-Type" : "application/x-www-form-urlencoded"}

# convert payload to URL-encoded form	
encodedPayload = ",".join(key + "=" + str(val) for key, val in payload.items())

#payload1 = {"grant_type": "client_credentials"}

authResponse = system.net.httpPost(url=authServerURL,postData=encodedPayload,headerValues = headerValues)

```

I tried passing payload, payload1 and encodedPayload as postData value in the http.post method but I get the same error -

IOError: Server returned HTTP response code: 400 for URL: MyURLToGenerateBearerToken

What am I writing wrong?  
Could anyone please help me with this?

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [August 14, 2023, 3:29pm UTC](https://forum.inductiveautomation.com/t/oauth-2-0-generate-token/77809/2 "2023-08-14T15:29:14Z")

</div>

The first thing I'd recommend is refactoring the above to use [`system.net.httpClient`](https://docs.inductiveautomation.com/display/DOC81/system.net.httpClient) to perform your POST request. The [`Response`](https://docs.inductiveautomation.com/display/DOC81/system.net.httpClient#system.net.httpClient-ResponseMethodsandAttributes) object you get back from the call might be able to give you more details on the 400 (Bad Request) error that you're getting from the server.

Ultimately, it is up to the endpoint you're working with to define how your request needs to be structured. If you already have a working example from Postman, do a careful comparison of where you're feeding in _URL Query Parameters_, _Request Headers_ and _Body/Payload_.

 ![2023-08-14 at 10.26 AM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1ddf493804b68d61d2193b86337e763b535eedb5.png)

Make sure to map these to the corresponding elements in your Ignition request. Below is a guess at a possible solution (impossible to really _know_ without more information on the endpoint you're communicating with or the example Postman request):

```python
# Configuration
authServerURL = "MyURLToGenerateBearerToken" # should be HTTPS
clientId = "MyClientId"
clientSecret = "MyClientSecret"
queryParams = {"grant_type": "client_credentials","client_id" : clientId,"client_secret":clientSecret}
headerValues = {"Content-Type" : "application/x-www-form-urlencoded"}

logger = system.util.getLogger("org.example.comms.someUsefulName")

# Send a POST request.
client = system.net.httpClient()
response = client.post(authServerURL, params=queryParams, headers=headerValues)
 
# Validate the response.
if response.good:
    logger.debugf("Request completed successfully, response code %d", response.statusCode)
    # ... do things you need to do with the `response`
else:
    logger.warnf("Request errored, response code %d: %s", response.statusCode, response.text)

```
