thingsboard icon indicating copy to clipboard operation
thingsboard copied to clipboard

[Bug]

Open Dev-22-goel opened this issue 3 years ago • 7 comments

Hi,

I was trying to fetch data from the thingsboard cloud server to my UI. I am using python REST API Client:

####################################
import logging
# Importing models and REST client class from Community Edition version
from tb_rest_client.rest_client_ce import *
# Importing the API exception
from tb_rest_client.rest import ApiException


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(module)s - %(lineno)d - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

# ThingsBoard REST API URL
url = "https://thingsboard.cloud"
# Default Tenant Administrator credentials
#my login credentials
Username = "####"
Password = "####"


# Creating the REST client object with context manager to get auto token refresh
with RestClientCE(base_url=url) as rest_client:
    try:
        rest_client.login(username=Username, password=Password)
        user = rest_client.get_user()
        devices = rest_client.get_customer_device_infos(customer_id=CustomerId('CUSTOMER', user.id), page_size=str(10),
                                                        page=str(0))
        logging.info("Devices: \n%r", devices)
    except ApiException as e:
        logging.exception(e)

###################################

and I am not able to get the data instead an error message throws up.

"""

PS C:\Users\devgoel\New folder1> & C:/Users/devgoel/AppData/Local/Programs/Python/Python37/python.exe "c:/Users/devgoel/New folder1/fetch_data.py"
2022-08-26 09:27:50 - ERROR - fetch_data - 28 - (401)
Reason: 
HTTP response headers: HTTPHeaderDict({'vary': 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', 'pragma': 'no-cache', 'expires': '0', 'content-type': 'application/json;charset=ISO-8859-1', 'content-length': '186', 'date': 'Fri, 26 Aug 2022 14:27:50 GMT'})
HTTP response body: b'{"message":"Authentication failed","errorCode":10,"status":401,"subscriptionErrorCode":null,"subscriptionEntry":null,"subscriptionValue":null,"timestamp":"2022-08-26T14:27:50.360+00:00"}'
Traceback (most recent call last):
  File "c:/Users/devgoel/New folder1/fetch_data.py", line 23, in <module>
    user = rest_client.get_user()
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\rest_client_ce.py", line 336, in get_user
    return self.auth_controller.get_user_using_get()
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api\api_ce\auth_controller_api.py", line 524, in get_user_using_get
    (data) = self.get_user_using_get_with_http_info(**kwargs)  # noqa: E501
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api\api_ce\auth_controller_api.py", line 591, in get_user_using_get_with_http_info
    collection_formats=collection_formats)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 406, 
in call_api
    _preload_content, _request_timeout)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 197, 
in __call_api
    _request_timeout=_request_timeout)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 427, 
in request
    headers=headers)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\rest.py", line 232, in GETrest.py", line 222, in request
    raise ApiException(http_resp=r)
tb_rest_client.rest.ApiException: (401)
Reason:
HTTP response headers: HTTPHeaderDict({'vary': 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', 'pragma': 'no-cache', 'expires': '0', 'content-type': 'application/json;charset=ISO-8859-1', 'content-length': 
'186', 'date': 'Fri, 26 Aug 2022 14:27:50 GMT'})
HTTP response body: b'{"message":"Authentication failed","errorCode":10,"status":401,"subscriptionErrorCode":null,"subscriptionEntry":null,"subscriptionValue":null,"timestamp":"2022-08-26T14:27:50.360+00:00"}'

"""

Is anyone facing this issue?

Any advice to this will be deeply appreciated.

OS: Windows ThingsBoard CE Python: 3.7.9

Dev-22-goel avatar Aug 26 '22 14:08 Dev-22-goel

###code update

import logging
# Importing models and REST client class from Community Edition version
from tb_rest_client.rest_client_ce import *
# Importing the API exception
from tb_rest_client.rest import ApiException


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(module)s - %(lineno)d - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

# ThingsBoard REST API URL
url = "http://thingsboard.cloud"
# Default Tenant Administrator credentials
Username = "####"
Password = "####"

# Creating the REST client object with context manager to get auto token refresh
with RestClientCE(base_url=url) as rest_client:
    try:
        rest_client.login(username=Username, password=Password)
        user = rest_client.get_user()
        devices = rest_client.get_customer_device_infos(customer_id=CustomerId('CUSTOMER', user.id), page_size=str(10),
                                                        page=str(0))
        logging.info("Devices: \n%r", devices)
    except ApiException as e:
        logging.exception(e)

Following is the error..

First it says:

No username password provided but I did provide the username and password. I believe it it authentication one

PS C:\Users\devgoel\New folder1> & C:/Users/devgoel/AppData/Local/Programs/Python/Python37/python.exe "c:/Users/devgoel/New folder1/test1.py"
2022-08-26 12:17:09,304 ERROR No username or password provided!
2022-08-26 12:17:09 - ERROR - rest_client_base - 94 - No username or password provided!
2022-08-26 12:17:09 - ERROR - test1 - 27 - (401)
Reason:
HTTP response headers: HTTPHeaderDict({'vary': 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', 'pragma': 'no-cache', 'expires': '0', 'content-type': 'application/json;charset=ISO-8859-1', 'content-length': '186', 'date': 'Fri, 26 Aug 2022 17:17:09 GMT'})
HTTP response body: b'{"message":"Authentication failed","errorCode":10,"status":401,"subscriptionErrorCode":null,"subscriptionEntry":null,"subscriptionValue":null,"timestamp":"2022-08-26T17:17:09.340+00:00"}'
Traceback (most recent call last):
  File "c:/Users/devgoel/New folder1/test1.py", line 22, in <module>
    user = rest_client.get_user()
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\rest_client_ce.py", line 336, in get_user
    return self.auth_controller.get_user_using_get()
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api\api_ce\auth_controller_api.py", line 524, in get_user_using_get
    (data) = self.get_user_using_get_with_http_info(**kwargs)  # noqa: E501
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api\api_ce\auth_controller_api.py", line 591, in get_user_using_get_with_http_info
    collection_formats=collection_formats)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 406, 
in call_api
    _preload_content, _request_timeout)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 197, 
in __call_api
    _request_timeout=_request_timeout)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\api_client.py", line 427, 
in request
    headers=headers)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\rest.py", line 232, in GET    query_params=query_params)
  File "C:\Users\devgoel\AppData\Local\Programs\Python\Python37\lib\site-packages\tb_rest_client\rest.py", line 222, in request
    raise ApiException(http_resp=r)
tb_rest_client.rest.ApiException: (401)
Reason:
HTTP response headers: HTTPHeaderDict({'vary': 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', 'pragma': 'no-cache', 'expires': '0', 'content-type': 'application/json;charset=ISO-8859-1', 'content-length': '186', 'date': 'Fri, 26 Aug 2022 17:17:09 GMT'})
HTTP response body: b'{"message":"Authentication failed","errorCode":10,"status":401,"subscriptionErrorCode":null,"subscriptionEntry":null,"subscriptionValue":null,"timestamp":"2022-08-26T17:17:09.340+00:00"}'

Dev-22-goel avatar Aug 26 '22 17:08 Dev-22-goel

Hi, do you sure that this user has access to RestAPI? If you use Swagger - is this request have 200 response?

ban2derlog avatar Aug 29 '22 17:08 ban2derlog

i'll check on swagger UI.. can you also tell me how can I check again if I have access to restapi

Dev-22-goel avatar Aug 29 '22 18:08 Dev-22-goel

Checked on Swagger:

Code 200: OK Code 401: Unauthorized Code 401: Unauthorized (Expired credentials)

but still not able to use on the python console..getting the same authentication error

Dev-22-goel avatar Aug 29 '22 19:08 Dev-22-goel

i was able to fix this big issue, but I would like to understand how i can fetch data using api's on my personal ui

Dev-22-goel avatar Aug 31 '22 15:08 Dev-22-goel

@Dev-22-goel Hi, how do you resolve this in code?

About transfer to your personal TB instance - all data you can fetch from Cloud can be POST to your Instance by RestAPI calls. But there can be issues if you instance has version <3.3 (our recommendation is always to have the latest). You can gather and transfer all in hierarchy: Tenant>Customer>Devices. Unfortunately, for now there isn't any method out-of-box to migrate from Cloud to Personal instance.

ban2derlog avatar Sep 16 '22 15:09 ban2derlog

Hi @ban2derlog

I was able to fix the authentication issue, I but I can't see a way to fetch old data from TB cloud. It only fetched the telemetry data.

Dev-22-goel avatar Sep 16 '22 16:09 Dev-22-goel

hi, you can use Version control to migrate all structure for your Tenant from cloud to different instance. https://thingsboard.io/docs/pe/user-guide/version-control/

ban2derlog avatar Jan 13 '23 13:01 ban2derlog