RESTful API

Java Example Call

Guidelines

This is a basic example in Java to build the proper URL and make the call.

  1. The first thing you are going to need is your public and private key from Ogangi; you can request your credentials with your Ogangi’s representative.
  2. Then you will need the signature method, we provide one in Java on the example code located at the end. The method is called signHmacSHA256
  3. Locate in your destination URL the parameters you are going to use, only those parameters are used in the signature process. For example, if you are calling the login method your signature will be built with {instanceId}/{user}/{password} as follow: 8/peter/peterpassword
  4. Note that the given private key is only used at the signature moment at your end, you will never pass the private key in any call, for that you will use the public key.
  5. Get the signature by using the signHmacSHA256 method. Ex: signHmacSHA256( “8/peter/ peterpassword”, “PRIVATE_KEY”.getBytes());
  6. Build the entire URL and make the call as the following example:

http://customer.ogangi.com/mmc/rest/api/login/8/peter/peterpassword/PUBLICKEY/signature


Code Sample


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
public class ExampleCall {
 
	public static void main(String[] args) {
		String base = "http://customer.ogangi.com/mmc/rest/api/";
		String method = "login/";
		String params = "8/peter/peterpassword";
		String keypublic = "4BFB-4943-9546-9BB8964254F2";
		String keyprivate = "FBF3-4310-909C-C4BBC26F886F";
		String signature = signHmacSHA256(params, keyprivate.getBytes());
		System.out.println(call(base + method + params + "/" + keypublic + "/" + signature, "GET"));
	}
 
	public static String call(String sUrl, String method) {
		System.out.println(sUrl);
		String outputXml = "";
		try {
			URL url = new URL(sUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(method); 
			BufferedReader d = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line = "";
			while ((line = d.readLine()) != null) {
				outputXml += line;
			}
			d.close();
			conn.disconnect();
			return outputXml; 
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
 
	private static String signHmacSHA256(String message, byte[] key) {
		try {
			SecretKey signingKey = new SecretKeySpec(key, "HMACSHA256");
			Mac mac = Mac.getInstance("HMACSHA256");
			mac.init(signingKey);
			byte[] digest = mac.doFinal(message.getBytes("UTF-8"));
			char[] hexadecimals = new char[digest.length * 2];
			for (int i = 0; i < digest.length; ++i) {
				for (int j = 0; j < 2; ++j) {
					int value = (digest[i] >> (4 - 4 * j)) & 0xf;
					char base = (value < 10) ? ('0') : ('a' - 10);
					hexadecimals[i * 2 + j] = (char) (base + value);
				}
			}
			return new String(hexadecimals);
		} catch (Exception e) {
			e.printStackTrace();
			return message;
		}
	}
 
}