HI, i am trying to get the sensor data from arduino IOT cloud dashboard to another site, and therefore trying to connect REST API through Python. Im using portenta H7 board, and following this tutorial https://docs.arduino.cc/arduino-cloud/api/arduino-iot-api/#api-keys--authentication
I have tried through POSTMAN as well as Python, but none of them seems to be working. on Python I get the access token but when try to run the code after that, it says unauthorized. in Postman, it says unauthorized too.
Can someone please help?
Hi, can you post an example of python code you are using?
Thanks
Hi, thank you for your response:
I am using this first:
from requests_oauthlib import OAuth2Session
from os import access
import iot_api_client as iot
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
# Get your 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",
)
# store access token in access_token variable
access_token = token.get("access_token")```
Then:
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = access_token
client = iot.ApiClient(client_config)
And then:
thing_id = "YOUR_THING_ID"
# as an example, interact with the properties API
api = iot.PropertiesV2Api(client)
try:
resp = api.properties_v2_list(thing_id)
print(resp)
except ApiException as e:
print("Got an exception: {}".format(e))```
Which gives me this error:
Got an exception: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Thu, 27 Jun 2024 02:02:16 GMT', 'Content-Type': 'application/vnd.goa.error+json', 'Content-Length': '185', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Expires': '0', 'Pragma': 'no-cache', 'Vary': 'Origin'})
HTTP response body: {"id":"pnbti4lC","code":"unauthorized","status":401,"detail":"not allowed to list the properties thingID=6e171f42-ed71-44a8-8425-0bff2724855d","meta":{"requestId":"ohQK3hDL3W-138032"}}```
Finally:
from requests_oauthlib import OAuth2Session
from os import access
import iot_api_client as iot
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
# Get your 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",
)
# store access token in access_token variable
access_token = token.get("access_token")
# configure and instance the API client with our access_token
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = access_token
client = iot.ApiClient(client_config)
thing_id = "YOUR_THING_ID"
# as an example, interact with the properties API
api = iot.PropertiesV2Api(client)
try:
resp = api.properties_v2_list(thing_id)
print(resp)
except ApiException as e:
print("Got an exception: {}".format(e))```
Here is a modified version of your script. It connects to API and list properties.
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
import iot_api_client as iot
import iot_api_client.apis.tags.properties_v2_api as propertyApi
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
# Get your token
client_id = "<client id>"
client_secret="<secret>"
oauth_client = BackendApplicationClient(client_id=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=client_id,
client_secret=client_secret,
include_client_id=True,
audience="https://api2.arduino.cc/iot",
)
# store access token in access_token variable
access_token = token.get("access_token")
# configure and instance the API client with our access_token
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = access_token
client = iot.ApiClient(client_config)
thing_id = "<thing_id>"
# as an example, interact with the properties API
api = propertyApi.PropertiesV2Api(client)
try:
resp = api.properties_v2_list(path_params={'id': thing_id})
if resp.response.status==200:
for property in resp.body:
id = property["id"]
name = property["name"]
ptype = property["type"]
value = property["last_value"]
print(f"Property: {name}::{ptype}={value}")
print("------------")
except ApiException as e:
print("Got an exception: {}".format(e))
You can find more example in the client git repository.
Thanks
Thank you for sharing this. Could you please let me know what libraries are needed? I am getting these errors:
ModuleNotFoundError Traceback (most recent call last)
Cell In[60], line 7
5 from iot_api_client.rest import ApiException
6 from iot_api_client.configuration import Configuration
----> 7 import iot_api_client.apis.tags.things_v2_api as thingApi
8 import iot_api_client.apis.tags.properties_v2_api as propertiesApi
9 import iot_api_client.apis.tags.series_v2_api as seriesApi
ModuleNotFoundError: No module named 'iot_api_client.apis'`
I have installed all these libraries:
oauthlib
requests-oauthlib
arduino-iot-client
typing_extensions
frozendict ~= 2.3.4
wheel
i used this code after generating token:
import iot_api_client as iot
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
# Configure the API client
client_config = Configuration(host="https://api2.arduino.cc/iot")
client_config.access_token = 'access token'
# Create an instance of the API client
client = iot.ApiClient(client_config)
# Set the organization ID as a header
org_id = "id"
client.set_default_header("X-Organization", org_id)
# As an example, interact with the devices API
devices_api = iot.DevicesV2Api(client)
try:
devices_list = devices_api.devices_v2_list()
# Assuming devices_list is a list of devices
for device in devices_list:
print(f"Device ({device.id}): {device.name}") # Adjust property names accordingly
except ApiException as e:
print(f"Exception when calling DevicesV2Api: {e}")
it listed all my devices associated with the organization space. now I want to get the properties/variables from a specific device, however, I couldn't find a resource for that.
Also, I'm using Insomnia with the cURL command from this source: Arduino IoT Cloud API..#api-_
with Express serve on cloud. i can get the token from there too:
now when I use this
curl --request GET \
--url https://api2.arduino.cc/iot/v2/things/{YOUR_THING_ID}/properties \
--header 'Authorization: Bearer YOUR_TOKEN'
for listing thing properties, it gives me an unauthorized error.
Could someone please provide some resources? My end goal is to get the data from my things from Arduino IoT Cloud's dashboards and variables and customize it to make an analytics dashboard.