import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class TestReadAPI { public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException { System.out.println("Start API Call ..."); System.out.println("--------------------"); // Set the timezone to UTC for 1WorldSync API call TimeZone timeZone = TimeZone.getTimeZone("UTC"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); dateFormat.setTimeZone(timeZone); String timeStamp = dateFormat.format(new Date()); System.out.println("Current DateTime: [" + timeStamp + "]"); String appID = "abc123xy"; // Please update with your APP ID here (8 characters) System.out.println("AppID: [" + appID + "]"); String secretKey = "qwertyuiopasdfghjklzxcvbnm123456"; // Please update with your Secret Key here (32 characters) String gln = "1234567890123"; // Please update with your 13-digit GLN here (13 digits) System.out.println("GLN: [" + gln + "]"); String uri = "/V1/product/fetch?timestamp=" + timeStamp + "&pageSize=1"; String hashInBase64 = generateHash(uri, secretKey); String hashInBase64URIencoded = urlencode(hashInBase64); String apiUrl = "https://content1-api.1worldsync.com" + uri; System.out.println("URL: [" + apiUrl + "]"); System.out.println("--------------------"); try { URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("appId", appID); connection.setRequestProperty("hashcode", hashInBase64); connection.setDoOutput(true); String postData = "{}"; try (OutputStream os = connection.getOutputStream()) { byte[] input = postData.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; StringBuilder response = new StringBuilder(); while ((line = br.readLine()) != null) { response.append(line); } System.out.println("API Response: [" + response.toString() + "]"); } } catch (Exception e) { e.printStackTrace(); } System.out.println("--------------------"); System.out.println("End API Call"); } private static String generateHash(String data, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException { Mac sha256Hmac = Mac.getInstance("HmacSHA256"); SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); sha256Hmac.init(secretKeySpec); byte[] hash = sha256Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8)); return java.util.Base64.getEncoder().encodeToString(hash); } private static String urlencode(String value) { try { return java.net.URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); } catch (java.io.UnsupportedEncodingException ex) { throw new RuntimeException(ex.getCause()); } } } // SAMPLE OUTPUT // Start API Call ... // -------------------- // Current DateTime: [2023-12-19T20:15:08Z] // AppID: [abc123xy] // GLN: [1234567890123] // URL: [https://content1-api.1worldsync.com/V1/product/fetch?timestamp=2023-12-19T20:15:08Z&pageSize=1] // -------------------- // API Response: [{"items":[{"objId":"123456","gln":"1234567890123","item":{ etc..... // -------------------- // End API Call