Thingsboard-REST API上的身份验证错误(FROM Curl命令和ASP.NET)

时间:2018-11-22 13:51:11

标签: thingsboard

我可以使用我的凭据登录Thingsboard网站,但是当我尝试使用CURL命令进行连接时,出现“身份验证失败”错误。

curl -X POST "https://cloud.thingsboard.io/api/auth/login" -d "{"username":"XYZPQR@ABCDLMN.com", "password":"Q@34&pwn"}" --header "Content-Type: application/json" --header "Accept: application/json"

错误代码

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

但是,当我在ASP.NET应用程序中使用相同的用户ID和密码来获取授权令牌时,我确实获得了JWT令牌,但是使用相同的令牌时,我无法从Thingsboard进行任何REST API调用。

ASP.NET核心代码

var response = new HttpResponseMessage();
var client = new HttpClient();                
UserModel model = new UserModel { username = "XYZPQR@ABCDLMN.com", password = "Q@34&pwn" };
var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");

response = await client.PostAsync("https://cloud.thingsboard.io/api/auth/login", content);

string data = await response.Content.ReadAsStringAsync();

var userToken = JsonConvert.DeserializeObject<UserToken>(data);

MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue("application/json");

client.DefaultRequestHeaders.Accept.Add(contentType);

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userToken.token);
Uri url = new Uri("https://cloud.thingsboard.io/api/plugins/telemetry/DEVICE/431be6e0-e8ca-11e8-9e5c-3d544ba4fdfc/values/timeseries?keys=Electricity");

response = await client.GetAsync(url);

模型类

 public class UserModel {
        public string username { get; set; }
        public string password { get; set; }
    }

    public class UserToken
    {
        public string token { get; set; }
        public string refreshToken { get; set; }
    }

请提出建议,如何从Thingsboard REST API获取遥测值。

1 个答案:

答案 0 :(得分:0)

这是我做的一个小错误;我将旧代码更改为新代码(如下所示),并且一切都按预期开始工作。

旧代码

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userToken.token);

新代码

client.DefaultRequestHeaders.Add("X-Authorization", "Bearer " + userToken.token);

感谢支持。