Search code examples
thingsboard

Thingsboard REST api always responds with status 401


First I got a token using curl command as shown here. Then used this token to authorize swagger and tried some endpoints, but all of them responded with

    {
      "status": 401,
      "message": "Authentication failed",
      "errorCode": 10,
      "timestamp": 1490619586352
    }

On server side I get this exception:

2017-03-27 13:31:16,149 [http-nio-0.0.0.0-8080-exec-9] ERROR o.t.s.s.s.m.token.RawAccessJwtToken - Invalid JWT Token io.jsonwebtoken.MalformedJwtException: Unable to read JSON value: ��!L��ȉ

I also tried this with curl, with the same results, using this syntax:

curl -X GET --header 'Accept: application/json' --header 'Content-Type: application/json' --header 'X-Authorization: MY_TOKEN' 'http://MY_SERVER:MY_PORT/api/tenants?limit=3'

where I changed MY_TOKEN, MY_SERVER and MY_PORT appropriately for my server.


Solution

  • It seems that a parameter(Bearer) was missing from one of the headers. It should be --header 'X-Authorization: Bearer MY_TOKEN'. When I added it the responses were as expected. So the complete command for curl is: curl -X GET --header 'Accept: application/json' --header 'Content-Type: application/json' --header 'X-Authorization: Bearer MY_TOKEN' 'http://MY_SERVER:MY_PORT/api/tenants?limit=3'