Reputation: 313
I am stuck in getting time-series data from ThingsBoard dashboard through an API call, I want to do the call from my Jypyter Notebook. I have tried some experiments on the Swagger UI, the Rest API of ThingsBoard but I am still can't find a way.
Can anyone help, please?
Kind Regards
Upvotes: 0
Views: 643
Reputation: 11
First you have to get a valid token with your user credentials. Following is the script to get it via python jupyter
import requests
import json
################## SET Credentials, URL and GET TOKEN ##################
username="yourUsername"
pw="yourPassword"
tokenUrl ="yourThingsboardUrl/api/auth/login"
headers={
"accept":"application/json",
"Content-Type":"application/json"
}
params={
"username":username,
"password": pw
}
response = requests.post('https://' +tokenUrl , headers=headers, data=json.dumps(params))
# SET token from response.json to token variable
token = response.json()['token']
After that you can attach the token variable to any REST API request like so:
response = requests.get("https://yourURL.com:yourPort/api/plugins/telemetry/DEVICE/"+ yourDeviceId + "/keys/timeseries", headers={'Content-Type': 'application/json', 'X-Authorization': 'Bearer {}'.format(token)})
Here I'm requesting alle the available keys for a specific device with "yourDeviceId"
After that do something like
data = response.json()
json_string = json.dumps(data)
To work with the data that you requested in an appropriate format
Upvotes: 1