简体   繁体   中英

How to get device Access token and perform multiple HTTP POST Request

I'm currently working on a project using an IoT platform " Thingsboard " where I've have created multiple devices, and I want to send data to each one of the devices from a JSON File, I'm using Rest Api to perform this request, but I've struggling for a while how to get the access token of my devices and parse each one of them in my request as a header param. I was just doing manually by getting them with Curl, but I want now to do it automatically. I know that Thingsboard has a Rest client Api written in java ( https://thingsboard.io/docs/reference/rest-client/ ) so I've tried to use that in my script but I's not working. I'm new to working with Rest Api so if anybody can gie me a clue it would be so helpful.

here's a part of my code for the requests:

                private static String token;
         public String getToken() {
                return token;
            }

                String paramValue = "param\\with\\backslash";
                String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8");

                URL url2 = new URL("https://demo.thingsboard.io/api/v1/token/telemetry?token=$JWT_TOKEN");
                HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty(JWT_TOKEN_HEADER_PARAM, "Bearer" +token);

                conn.setDoOutput(true);
                OutputStream outStream = conn.getOutputStream();
                OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
                outStreamWriter.write(list.toString());
                outStreamWriter.flush();
                outStreamWriter.close();
                outStream.close();
                String response = null;

                System.out.println(conn.getResponseCode());
                System.out.println(conn.getResponseMessage());

                DataInputStream input1 = null;
                input1 = new DataInputStream (conn.getInputStream());
                while (null != ((response = input1.readLine()))) {
                    System.out.println(response);
                    input1.close ();
                }
            }
            
            catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }

If you are trying to get the JWT-token to authenticate the following java should work:

Request request = Request.Post("http://THINGSBOARD_URL/api/auth/login");
String body = "{\"username\":\"tenant@thingsboard.org\", \"password\":\"tenant\"}";
request.bodyString(body,ContentType.APPLICATION_JSON);
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
HttpResponse httpResponse = request.execute().returnResponse();
System.out.println(httpResponse.getStatusLine());
if (httpResponse.getEntity() != null) {
    String html = EntityUtils.toString(httpResponse.getEntity());
    System.out.println(html);
}

Don't get confused with JWT-Token for tenant authentication and Access-Token for Device Authentication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM