# API POST Token not works

**URL:** <https://forum.inductiveautomation.com/t/api-post-token-not-works/65113>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [October 12, 2022, 1:34pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113 "2022-10-12T13:34:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 12, 2022, 1:34pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/1 "2022-10-12T13:34:10Z")

</div>

Hi,

Script below doesn't work suchas it works on Postman.  
Response is 401.  
Is it an API problem or script problem ?

```auto
import system
import features
from urllib import urlencode
	
clientID = "myclient_id"
clientSECRET = "myclient_secret"
url = "https://site.example/access_token"
body = {
	"grant_type":"client_credentials"
	}
headers = {
	"Content-Type": "application/x-www-form-urlencoded",
	"Accept": "*/*",
	"Accept-Encoding": "gzip, deflate, br",
	"Connection": "keep-alive",
	"Authorization": "Basic xxxxxxxxx"
	}

output = system.net.httpPost(
		url=url, 
		contentType='application/x-www-form-urlencoded',
		postData = urlencode(body), 
		username = clientID, 
		password = clientSECRET, 
		headerValues = headers
		)
		

```

Thanks

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 12, 2022, 1:39pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/2 "2022-10-12T13:39:47Z")

</div>

Use `system.net.httpClient` instead of `system.net.httpPost`. It uses an entirely different (modern) engine that fixes many otherwise unfixable problems with the older functions.

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 12, 2022, 1:45pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/3 "2022-10-12T13:45:27Z")

</div>

It doesn't work too.  
I get an error 400.

```auto
client = system.net.httpClient()
payload = {
		"grant_type":"client_credentials",
		"client_id":clientID,
		"client_secret":clientSECRET
		}
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Authorization': 'Basic xxx'
  
}
res = client.post( url, payload, headers)

```

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [October 12, 2022, 2:28pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/4 "2022-10-12T14:28:42Z")

</div>

Can you post a snip of what your Postman window looks like? I've been bitten by this where Postman will automatically add some header that isn't visible and that will be the reason why it works there but not from an `httpClient`

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 12, 2022, 2:38pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/5 "2022-10-12T14:38:11Z")

</div>

Headers :

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/2/b2ba2f54b72e6259117c831efd97485ac904525f.png)

Body :

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/f/ef365ec2db8d03a0ad63bbae0627a56b297a9c5b.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 12, 2022, 2:39pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/6 "2022-10-12T14:39:32Z")

</div>

HttpClient doesn't currently properly encode `x-www-form-urlencoded` requests. Use `urllib` to encode your POST data to a string, then `httpClient()` should be able to send it.

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 12, 2022, 3:02pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/7 "2022-10-12T15:02:31Z")

</div>

It doesn't work with

```auto
from urllib import urlencode
res = client.post( url, urlencode(payload), headers)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 12, 2022, 3:29pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/8 "2022-10-12T15:29:37Z")

</div>

What does 'doesn't work' mean?  
Can you install Wireshark and look at the outgoing request and see if it matches what you expect?

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [October 12, 2022, 3:46pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/9 "2022-10-12T15:46:36Z")

</div>

you don't provide the scope = "/adict/v1" as in your other screenshot?

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 18, 2022, 1:50pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/10 "2022-10-18T13:50:13Z")

</div>

same result with scope in headers.

Wireshark doesn't see any request HTTP POST even if I use Postman.  
I use filter : http.request.method == “POST”

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 18, 2022, 2:54pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/11 "2022-10-18T14:54:56Z")

</div>

It works !  
My final script :  
Don't use httpClient()

```auto
import system
import features
from urllib import urlencode
import httplib
	
clientID = "user"
clientSECRET = "pa$$w0rd"
baseUrl = "mysite.com"
url = "/a/b/access_token"
body = {
	"scope":"/app/v1",
	"grant_type":"client_credentials",
	"client_id":clientID,
	"client_secret":clientSECRET
	
	}
headers = {
	'Content-Type': 'application/x-www-form-urlencoded',
	'Authorization': 'Basic xxx'
} 
conn = httplib.HTTPSConnection(baseUrl)
conn.request("POST", url, urlencode(body), headers)
res = conn.getresponse()
	
data = res.read()
result = system.util.jsonDecode(data.decode("utf-8"))
return result

```

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [October 21, 2022, 10:38am UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/12 "2022-10-21T10:38:09Z")

</div>

With httplib it seems you can't get reponse body with PUT and POST requests.  
Do you know an other way ?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 21, 2022, 12:30pm UTC](https://forum.inductiveautomation.com/t/api-post-token-not-works/65113/13 "2022-10-21T12:30:54Z")

</div>

Use httpClient with Paul's advice above.
