Script Console cryptography for HttpNtlmAuth requests.get Import Error

Struggling a bit trying to develop a HTTP get with domain authentication to work in Ignition. The end goal is to periodically read in a json from the API, parse out data, put it into a database for use by Ignition, and then post a modified file with some altered flags. After some research, I took a stab at using requests.get and HttpNtlmAuth from the requests_ntlm library. I used pip to install requests and requests_ntlm and their dependencies on my computer. I copied all the files from C:\Python27\Lib\site-packages to C:\Program Files\Inductive Automation\Ignition\user-lib\pylib, restarted the designer, and ran the following:

Note that individual requests.get and HttpNtlmAuth run without python errors.

It would seem like cryptography is missing a file, but that file appears to be present in my ignition cache (C:\Users[me].ignition\cache\gwlocalhost_8088\C0\pylib\site-packages\cryptography\hazmat\bindings top left), ignition site packages s (C:\Users[me].ignition\cache\gwlocalhost_8088\C0\pylib\site-packages\cryptography\hazmat\bindings top right), and my local python site packages at C:\Python27\Lib\site-packages\cryptography\hazmat\bindings (bottom).

The code works from the python command line (content of result is as expected):

I already tried the following:

  1. Restarting designer
  2. Deleting the C:\Users[me].ignition\cache directory
  3. Editing and saving the C:\Users[me].ignition\cache\gwlocalhost_8088\C0\pylib\site-packages\cryptography\hazmat\bindings\openssl\binding.py file after adding a whitespace in a commented line.

Any suggestions for fixing this import error or developing an API call that works like the one in my python command line would be much appreciated.

.pyd files are basically DLLs, and these don't work on Jython.

I think that Ignition still ships with the Apache HTTP client library in all scopes... your best bet might be to convert some sample code for using this library to Python/Jython, e.g. rest - Http post requests unsing NTLM Authentication (java) - Stack Overflow

Thanks for the tip. Got something working. Here's a screenshot for posterity:

Post code, not pictures of code. It helps with searching via text. Please see Wiki - how to post code on this forum. You can edit your post by clicking the pencil icon on the bottom right.

Edit: Code for those searching:

Original
url = 'MY URL'
un = 'MY USERNAME'
do = 'MY DOMAIN'
pwd = 'MY PASSWORD'


# Import
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.client.methods.HttpGet
import org.apache.http.util.EntityUtils


credProv	= org.apache.http.impl.client.BasicCredentialsProvider()	# Build credentials provider
ntCred		= org.apache.http.auth.NTCredentials(un, pwd, '', do)		# Build NT domain credentials
credProv.setCredentials(org.apache.http.auth.AuthScope.ANY, ntCred);	# Set credentials
bld 		= org.apache.http.impl.client.HttpClientBuilder.create()	# Create HttpClientBuilder
client		= bld.setDefaultCredentialsProvider(credProv).build()		# Build client with credential proxy

get 		= org.apache.http.Client.methods.HttpGet(url)	# Define http get
res			= client.execute(get)							# Execute get

# Result string
ress		= str(org.apache.http.util.EntityUtils.toString(res.getEntity(), 'UTF-8'))
Slimmed
from org.apache.http.auth import AuthScope, NTCredentials
from org.apache.http.impl.client import BasicCredentialsProvider, HttpClientBuilder
from org.apache.http.client.methods import HttpGet
from org.apache.http.util import EntityUtils

url = 'MY URL'
un = 'MY USERNAME'
do = 'MY DOMAIN'
pwd = 'MY PASSWORD'


credProv	= BasicCredentialsProvider()		# Build credentials provider
ntCred		= NTCredentials(un, pwd, '', do)	# Build NT domain credentials
credProv.setCredentials(AuthScope.ANY, ntCred);	# Set credentials

bld 		= HttpClientBuilder.create()							# Create HttpClientBuilder
client		= bld.setDefaultCredentialsProvider(credProv).build()	# Build client with credential proxy

get 		= HttpGet(url)			# Define http get
res			= client.execute(get)	# Execute get

# Result string
ress		= str(EntityUtils.toString(res.getEntity(), 'UTF-8'))
3 Likes