IoT Cloud API empty responses

Dear all
I have made a first project with an Arduino Nano 33 IoT. It's a simple setup with a sensor to measure bottom humidity and temperature of the environment. It works very well, dashboard is showing what I want to know and the variable updates are done every half hour as I've set them to do.

Now I'm trying to use the API, and that's where I'm stuck. For the calls I use Python call, I've added the code at the end of the post but removed the client id and the client secret for obvious reasons (my house temperature is none of Your business :slight_smile: ).
I receive a token, but all other requests give me an empty response. HTTP status of the HTTP request is 200, so I assume it's all ok. Could anyone tell me what I'm missing?

The request are simple:
https://api2.arduino.cc/iot/v2/things?across_user_ids=false
https://api2.arduino.cc/iot/v2/devices/5bb5c849-9f9d-4839-8ae5-ce3f2ea436f0/properties

#!/usr/bin/env python
# coding: utf-8

import requests
import json

def HandleStatusError(status_code, response):
    if status_code == 401:
        message = "401 - Unauthorized"
    elif status_code == 400:
        message = "400 - Application error: " + response.text
    else:
        message = str(status_code) + "Unknown status_code"
    print("Status: " + message)

#get a token
url = 'https://api2.arduino.cc/iot/v1/clients/token'
headers = {'content-type': 'application/x-www-form-urlencoded'}
data = {'grant_type':'client_credentials',
        'client_id':'YOUR CLIENT ID',
        'client_secret':'YOUR CLIENT SECRET',
        #'client_secret': 'foo',
        'audience':'https://api2.arduino.cc/iot'
       }

try:
    x = requests.post(url, data = data, headers = headers)
    assert x.status_code == 200, "Request failed"
    response = json.loads(x.text)
    token = response['access_token']
except AssertionError as e:
    print("ERROR! :" + str(e))
    HandleStatusError(x.status_code, x)

url = "https://api2.arduino.cc/iot/v2/things?across_user_ids=false"
headers = {'Authorization': 'Bearer '+token}
try:
    x = requests.options(url, headers = headers)
    assert x.status_code == 200, "Request failed"
except AssertionError as e:
    print("ERROR! :" + str(e))
print("headers: "+ str(x.headers))
print("body: "+ str(x.text))

url = "https://api2.arduino.cc/iot/v2/devices/5bb5c849-9f9d-4839-8ae5-ce3f2ea436f0/properties"
headers = {'Authorization': 'Bearer '+token}
x = requests.options(url, headers = headers)
print("headers: "+ str(x.headers))
print("body: "+ str(x.text))

Hello @bdeman,

there are a couple of issues in your code.

The first one is that you are calling

    x = requests.options(url, headers = headers)

which makes an OPTIONS request, and not a GET one. Try using

x = requests.request("GET", url, headers = headers)

Also, in the second request, you are calling

url = "https://api2.arduino.cc/iot/v2/devices/5bb5c849-9f9d-4839-8ae5-ce3f2ea436f0/properties"

that is related to device properties. In this case you need thing properties, so the correct URL should be:

url = "https://api2.arduino.cc/iot/v2/things/YOUR_THING_ID/properties"

Can apply the suggested fixes and let me know?

1 Like