Hi everyone,
I've written two scripts to automate the creation of channels, devices, tag groups, and tags in Kepware using its API.
- Script 1 : Uses API requests via system.net.httpClient().
- Script 2 : Uses Kepware's Python libraries (kepconfig).
Issue
When I run Script 1:
It works fine in:
- Local IDE (e.g., PyCharm, VS Code, etc.)
- Ignition's Script Console
- Directly on an Ignition component (e.g., button script)
However, when I move it to Ignition's Project Library and try executing the function, I get a 401 Unauthorized error.
What I've Tried
- Checked authentication details (same credentials work in other cases).
- Used both system.net.httpPost() and system.net.httpClient().post().
- Verified that Kepware is running and accessible.
- Confirmed that Script 2 (using kepconfig) works without issues.
Has anyone encountered this before? Could there be a difference in how Ignition handles authentication for scripts in the Project Library vs. direct execution?
Scripts Below for Reference:
(Script 1 - API requests)
import json
def createChannel(channelName, deviceDriver):
url = "http://127.0.0.1:57412/config/v1/project/channels"
payload = {
"common.ALLTYPES_NAME": channelName,
"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": deviceDriver
}
headers = {
'Authorization': 'Basic Q………….',
'Content-Type': 'application/json'
}
response = system.net.httpClient().post(url, headers=headers, data=json.dumps(payload))
print("Create Channel Response:", response.statusCode)
(Similar functions for device, tag group, and tag creation)
(Script 2 - Kepware libraries)
import kepconfig.connection as connection
import kepconfig.connectivity as connectivity
from kepconfig.connectivity import channel, device, tag
server = connection.server(host='127.0.0.1', port=57412, user='Administrator', pw='')
try:
channel_data = {"common.ALLTYPES_NAME": "ChannelY", "servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Modbus TCP/IP Ethernet"}
channel.add_channel(server, channel_data)
except:
print("Channel exists")
(Similar structure for device, tag group, and tag creation)
Version: 8.1.36
Any insights or suggestions would be greatly appreciated! Thanks in advance.