Command-line Java Example (Private M2M)

Following is a Java example call to the API with OAuth2 authentication. The example doesn't use any external libraries to make OAuth2 calls. You will have to update the key, secret, and the API Server URL to connect to the API server.

The code below can be downloaded by clicking on the following link: PrivateM2M Client Java Example.

  
 
 import java.io.DataOutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
 public class Main {
     public static void main(String[] argv) {
         String clientId = "YOUR_CLIENT_ID";
         String clientSecret = "YOUR_CLIENT_SECRET";
 
         String resourceServerUrl = "https://api.trakopolis.com";
         String accessTokenEndpoint = "https://api.trakopolis.com/api/oauth2/token";
 
         URL url;
         HttpURLConnection connection = null;
         try {
             // Create connection
             url = new URL(accessTokenEndpoint);
             String urlParameters = "grant_type=client_credentials";
             connection = (HttpURLConnection)url.openConnection();
             connection.setRequestMethod("POST");
             connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
             String credentials = clientId + ":" + clientSecret;
             connection.setRequestProperty("Authorization", "Basic " +
                     javax.xml.bind.DatatypeConverter.printBase64Binary(credentials.getBytes()));
 
             connection.setUseCaches (false);
             connection.setDoInput(true);
             connection.setDoOutput(true);
 
             // Send request
             DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
             wr.writeBytes (urlParameters);
             wr.flush ();
             wr.close ();
 
             // Get Response for receiving access token
             java.io.InputStream is = connection.getInputStream();
             java.io.BufferedReader rd = new java.io.BufferedReader(new java.io.InputStreamReader(is));
             String line;
             StringBuffer response = new StringBuffer();
             while((line = rd.readLine()) != null) {
                 response.append(line);
                 response.append('\r');
             }
             rd.close();
             String serverResponse = response.toString();
             System.out.printf("Server response with token. JSON: " + serverResponse);
             TokenInfo tokenInfo = new com.google.gson.Gson().fromJson(serverResponse, TokenInfo.class);
 
             // Example for calling list of assets
             url = new URL(resourceServerUrl + "/api/assets");
             connection = (HttpURLConnection)url.openConnection();
             connection.setRequestProperty("Authorization", "Bearer " + tokenInfo.access_token);
             connection.setRequestProperty("request-type", "api");
 
             // You may use XML output as well
             connection.setRequestProperty("response-type", "application/json");
 
             // Restrict result size to 5. By default most entities have limit = 1000
             connection.setRequestProperty("result-size", "5");
             connection.setRequestProperty("Content-Type", "application/json");
 
             // Get response for receiving list of assets
             is = connection.getInputStream();
             rd = new java.io.BufferedReader(new java.io.InputStreamReader(is));
             response = new StringBuffer();
             while((line = rd.readLine()) != null) {
                 response.append(line);
                 response.append('\r');
             }
             rd.close();
             serverResponse = response.toString();
             System.out.printf("Assets retrieved (5 max). JSON: " + serverResponse);
 
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if(connection != null) {
                 connection.disconnect();
             }
         }
     }
 
     public class TokenInfo {
         public String access_token;
         public String refresh_token;
         public String expires_in;
         public String scope;
     }
 }