I'm using an arduino Opla (MKR WiFi 1010). I can change the value on the dashboard, and I can get the updated value in python, but I can't change the value in python. I'm developing this without my Arduino running, but this shouldn't be a problem, since the value can be updated from the dashboard.
My python code:
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
import iot_api_client as iot
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
YOUR_CLIENT_ID = "iue"
YOUR_CLIENT_SECRET = "Lnk"
YOUR_THING_ID = "e5d"
DASHBOARD_ID = "49d"
THING_ID2 = "08b"
message_update_id = "db7"
light_alarm_id = "f89"
movement_alarm_id = "c10"
shake_alarm_id = "8cf"
stuck_id = "b3c"
def get_token():
oauth_client = BackendApplicationClient(client_id=YOUR_CLIENT_ID)
token_url = "https://api2.arduino.cc/iot/v1/clients/token"
oauth = OAuth2Session(client=oauth_client)
token = oauth.fetch_token(
token_url=token_url,
client_id=YOUR_CLIENT_ID,
client_secret=YOUR_CLIENT_SECRET,
include_client_id=True,
audience="https://api2.arduino.cc/iot",
)
print(token)
return token.get("access_token")
def get_device(access_token):
# configure and instance the API client
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = access_token
client = iot.ApiClient(client_config)
# as an example, interact with the devices API
devices_api = iot.DevicesV2Api(client)
try:
resp = devices_api.devices_v2_list()
print(resp)
except ApiException as e:
print("Got an exception: {}".format(e))
def change_value(access_token, thing_id, property_id):
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = access_token
client = iot.ApiClient(client_config)
# as an example, interact with the devices API
devices_api = iot.PropertiesV2Api(client)
try:
api_response = devices_api.properties_v2_show(thing_id, property_id)
print(api_response)
resp = devices_api.properties_v2_publish(thing_id, property_id, {"value": True})
print(resp)
except ApiException as e:
print("Got an exception: {}".format(e))
if __name__ == "__main__":
access_token = get_token()
print(access_token)
# get_device(access_token)
change_value(access_token, YOUR_THING_ID, light_alarm_id)
The response I get is:
{'access_token': 'eyJ', 'expires_in': 300, 'token_type': 'Bearer', 'expires_at': 1690049149.4899123}
eyJ
{'created_at': datetime.datetime(2023, 7, 11, 12, 49, 15, 508209, tzinfo=tzutc()),
'deleted_at': None,
'href': '/iot/v1/things/08b/properties/f89',
'id': 'f89',
'last_value': False,
'max_value': None,
'min_value': None,
'name': 'light_alarm',
'permission': 'READ_WRITE',
'persist': True,
'sync_id': None,
'tag': 5.0,
'thing_id': '08b',
'thing_name': 'Security Test ',
'type': 'STATUS',
'update_parameter': 0.0,
'update_strategy': 'ON_CHANGE',
'updated_at': datetime.datetime(2023, 7, 11, 12, 49, 15, 508209, tzinfo=tzutc()),
'value_updated_at': datetime.datetime(2023, 7, 22, 18, 0, 43, 788000, tzinfo=tzutc()),
'variable_name': 'light_alarm'}
Got an exception: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, no-store, must-revalidate', 'Content-Type': 'application/vnd.goa.error+json', 'Date': 'Sat, 22 Jul 2023 18:00:51 GMT', 'Expires': '0', 'Pragma': 'no-cache', 'Vary': 'Accept-Encoding, Origin', 'Content-Length': '187', 'Connection': 'keep-alive'})
HTTP response body: {"id":"g2/A8tSj","code":"unauthorized","status":401,"detail":"not allowed to publish the property thingID=e5d","meta":{"requestId":"zasZuNcmjX-2438832"}}
When running with curl, the same happens:
curl -X PUT "https://api2.arduino.cc/iot/v2/things/e5d/properties/f89/publish" --data-raw "{\"value\": true}" -H 'Content-Type: application/json' -H "Authorization: Bearer eyJ" --ssl-no-revoke
Any help would be greatly appreciated.