2
votes

I've created a IBM Watson IoT platform, and a device type "testdevice".

I would like a simple test where I push data via a Python script and the HTTP API.

So far I've managed to be able to push data, but getting:

(403, 'Not allowed')

I can see in the IBM Watson IoT log that my computer has tried to push data, but Authentication failed.

As a request header I'm using auth=('use-token-auth', 'MY-TOKEN') so the entire code is:

import requests

response = requests.post('http://MY-ORG-ID.messaging.internetofthings.ibmcloud.com:1883/api/v0002/device/types/testdevice/devices/MY-DEVICE/events/test', data={'number': 1}, auth=('use-token-auth', 'MY-TOKEN'), headers={'Content-type': 'application/json'})

print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP

What could be the reason for authentication failing?

2

2 Answers

1
votes

Have you tried connecting over HTTPS? Since the middle of the year, by default, organizations are configured to reject insecure connections, unless a user specifically enables this (you can find this option in the setting panel in the dashboard if you want to enable unencrypted connectivity in your organization), this is one possible reason you are getting 403 not allowed responses.

FYI, you might also be interested in the Python client library, which supports a HTTP only connection as well as the more feature rich MQTT client:

pip install ibmiotf

import ibmiotf.device

options = {"org": orgId, "type": "testdevice", "id": "MY-DEVICE", "auth-method": "token" , "auth-token": "MY-TOKEN"}
client = ibmiotf.device.HttpClient(options)

data={'number': 1}
client.publishEvent("test", "json", data)
0
votes

Your code does work for me, so could be as DavidParker says http is not allowed. It could also be your token is getting messed up. I always encode the authentication header - x="use-token-auth:password".encode('base64') `

Or, that token you have, is wrong.