RESTful API

JSON Broadcast Example Call

Guidelines

This page presents two examples, one on PHP and the other in JAVA, for properly making 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 createBroadcastFromJson method your signature will be built with {instanceId}/{broadcastId}/{shortcode}/{userName}/{shortcodeIgnore}/{campaignName}/{date} as follow: 8/-1/434/peter/true/testJsonBroadcast/null
  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/-1/434/peter/true/testJsonBroadcast/null”, “PRIVATE_KEY”.getBytes());
  6. Build the entire URL and make the call as the following example:

http://customer.ogangi.com/mmc/rest/api/createBroadcastFromJson/8/-1/434/peter/true/testJsonBroadcast/null/d1F2T3Y4N56/signature


Java Call Sample


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
public static void main(String[] args) { 
		String base = "http://customer.ogangi.com/mmc/rest/api/";
		String method = "createBroadcastFromJson/";
		String params = "8/-1/434/peter/true/testJsonBroadcast/null";
		String keypublic = "d1F2T3Y4N56";
		String keyprivate = "A1B2T3J4N5";
 
		String signature = signHmacSHA256(params, keyprivate.getBytes());
		System.out.println(call(base + method + params + "/" + keypublic + "/" + signature, "POST"));
	}
 
	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.setRequestProperty( "Content-Type", "application/json" );
			conn.setRequestProperty("Accept", "application/json");
			conn.setRequestMethod(method); 
 
			OutputStream os = conn.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
			osw.write("[{\"mobilenumber\":\"00000000\",\"text\":\"Test1 createBroadcastFromJson\"},"
                                        + "{\"mobilenumber\":\"00000000\","
					+ "\"text\":\"Test2 createBroadcastFromJson\"}]");
			osw.flush();
			osw.close();
 
 
			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;
		}
	}

PHP Call Sample


<?php
 
$data = '[{"mobilenumber":"00000000","text":"Test1 createBroadcastFromJson"},{"mobilenumber":"00000000","text":"Test2 createBroadcastFromJson"}]';
 
$ch = curl_init();
 
curl_setopt($ch,CURLOPT_URL,"http://customer.ogangi.com/mmc/rest/api/createBroadcastFromJson/8/-1/434/peter/true/testJsonBroadcast/null/d1F2T3Y4N56/signature");
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result = curl_exec ($ch);
 
echo $result;
?>