Read the details here to find out how this works. The list of available calls is also available here. You may well also find using one of the publicly available requests is sufficient for your requirements.
private final String API_KEY = "API_KEY"; private final String SECRET = "THE_KEY_SECRET"; private final String USER_ID = "1234567"; // The system time can be used as a nonce value public String nextNonce() { return String.valueOf(System.currentTimeMillis()); } // Note the signature string returned is correctly base 64 UTF8 encoaded public String buildSignature( final String nonce) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { SecretKeySpec keySpec = new SecretKeySpec(SECRET.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); byte[] hash = mac.doFinal(nonce.concat(USER_ID).concat(API_KEY).getBytes()); ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); for (byte b : hash) { ps.format("%02x", b); } return os.toString("UTF8"); }
In this example the standard javax.json 7 libraries are used to process the server response. There is no need to use any third party libraries. Depending on your requirements you may wish to modify the call method to return a json structure directly. In this example for simplicity we return the json formatted string.
public String remoteCall(final URL path ) throws IOException, NoSuchAlgorithmException, InvalidKeyException { // A new nonce and signature is built from code in the above example String nonce = nextNonce(); String signature = buildSignature(nonce); // Signature nonce and key parameters are appended to the URL request StringBuilder content = new StringBuilder(200); content.append("nonce=").append(URLEncoder.encode(nonce, "UTF-8")) .append("&key=").append(URLEncoder.encode(API_KEY, "UTF-8")) .append("&signature=").append(URLEncoder.encode(signature, "UTF-8")); HttpsURLConnection con = (HttpsURLConnection) path.openConnection(); // Content Type and method headers must be set correctly con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); try (OutputStream out = con.getOutputStream()) { out.write(content.toString().getBytes()); out.flush(); } if (con.getResponseCode() != HttpURLConnection.HTTP_OK) { JsonObjectBuilder error = Json.createObjectBuilder(); error.addNull("result"); error.add("error", con.getResponseMessage()); return error.build().toString(); // OK } else { try (InputStream incomming = con.getInputStream()) { JsonStructure repply; try (JsonReader reader = Json.createReader(incomming)) { repply = reader.read(); return repply.toString(); } } } }
public void printMyBalance() throws Exception { URL request = new URL("https://nzbcx.com/api/account/balance"); System.out.println(remoteCall(request)); }
API_KEY = 'MY_API_KEY' API_SECRET = 'THE_BIG_SECRET' USER_ID = '1234567' nonce = str(int(time.time())) sigtext = nonce + USER_ID + API_KEY signature = hmac.new(API_SECRET, msg=sigtext, digestmod=hashlib.sha256).hexdigest()
$key = 'KEY_TEXT_HERE'; $secret = 'SECRET_TEXT_HERE'; $id = '1234567'; // Your User ID // Nonce of the current Unix timestamp string encoded $nonce = strval(time()); $sigtext = $nonce . $id . $key; $signature = strtoupper( hash_hmac ('sha256',$sigtext,$secret));
$prams = array( 'nonce' => $nonce, 'key' => $key, 'signature' => $signature , ); // Only secure requests over HTTPS are accepted $url = 'https://nzbcx.com/api/account/balance'; // The request must be be well formed. // content type and method headers must be set $request = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($prams,'', '&'), ), );
$context = stream_context_create($request); $response = file_get_contents($url, false, $context); var_dump($response);
$request = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($params) ), // Add ssl prams 'ssl'=>array( 'allow_self_signed'=>true, 'verify_peer'=>false, ),Contribute Let us know if you have other coding examples and we will post your comments and credits on the site The examples here are posted for information purposes only. Send us your feedback