Using system.net.httpGet To Call For Bearer Token

Hello,

I am completely new to Python and Ignition, and am having a heck of a time trying to put together a simple call to receive a bearer token. Here’s what I have:

url2 = “http://ipaddress/api/v1/signin
urlHeader = {“Accept”: “application/json”, “username”: “user”, “password”: “password”, “Content-Type”: “application/json”}

response = system.net.httpGet(url2, headerValues=urlHeader)
data = system.util.jsonDecode(response)

print data

I am getting the following error:

Traceback (most recent call last):
File “”, line 3, in
IOError: [Errno 2] File not found - http://ipaddress/api/v1/signin

I had this working using Invoke-RestMethod and powershell, but I just can’t seem to get this figured out. Any help is greatly appreciated.

Mikael

I had similar problems getting a token in v7.9.x and resolved them by using the URLLIB2 and URLLIB libraries.
The syntax is similar and they work for me where system.net.httpGet and system.net.httpPost didn’t.
Note: system.net.httpGet works fine for any GET that returns JSON/XML data except for getting a token. For some reason, I could never get a token without going to URLLIB2.

I also had problems with system.net.post that were also resolved with URLLIB2 and URLLIB

Something to watch for is garbage collection on the urllib2.urlopen. It seemed that remnants were left behind in memory and although there is very little to find about closing them, our system became unstable and needed a restart every 20 to 30 days depending on workload. We have not had a problem since I added the close statements.

Here’s a sample of what works for me for getting the token as well as posting data.

	#import libraries
	import urllib	
	import urllib2
	from urllib2 import URLError, HTTPError	# this is for error handling URLError, HTTPError, IOError
	...
	...
	#construct endpoint and parameters
	endPoint= endPointAddress
	postData= {'equipmentId':equipmentId,'station':station,'quantity':quantity,'print':printFlag}
	header	= {'Authorization': 'Bearer %s' %accessToken}
	headerNoToken = {'Authorization': 'Bearer currentToken'}
	data	= urllib.urlencode(postData)

	try:
		apiRequest 	= urllib2.Request(endPoint, data, header)
		apiResponse	= urllib2.urlopen(apiRequest)
		qctResponse	= apiResponse.read()
		apiResponse.close()		# actively close the connection JIRA Issue: QCT-1678
		jsonResponse= system.util.jsonDecode(qctResponse)
		if jsonResponse != None:
			...
			...
		else:
			...
			...
	except HTTPError, he:
		...
		...
	except URLError, ue:	# next version Jython recommends "URLError as <alias>" instead of "URLError, alias"
		...
		...
	except IOError, ioe:
		...
		...
	except:
		...
		...
2 Likes

Awesome, thanks for your reply. I’ll give it a go.