Java
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class RFGAPI
{
private static String apiURL = "https://sandbox.saysoforgood.com/API";
private static String apid = ""; // put your key here
private static String secretHex = ""; // put your secret here
public static void main(String[] args) throws Exception {
String command = "{ \"command\" : \"test/copy/1\" , \"data1\" : \"THIS IS A TEST\"}";
String response = sendCommand(command);
System.out.println(response);
}
public static String sendCommand(String command) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
long time = System.currentTimeMillis()/1000;
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(DatatypeConverter.parseHexBinary(secretHex),"HmacSHA1"));
byte[] hash = mac.doFinal((time + command).getBytes(Charset.forName("UTF-8")));
String url = apiURL + "?apid="+apid+"&time="+time+"&hash="+DatatypeConverter.printHexBinary(hash);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Length", String.valueOf(command.length()));
OutputStream os = con.getOutputStream();
try {
os.write(command.getBytes("UTF-8"));
os.flush();
} finally {
os.close();
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
StringBuilder buffer = new StringBuilder();
try {
while ((line = in.readLine()) != null) {
buffer.append(line);
}
} finally {
in.close();
}
return buffer.toString();
}
}