Loading...
 

JAVA Code Example


Sample Authorization Key Generation

This is a sample authorization that must be used in all requests against the API.

/**
 * Returns the Encoded string used to authenticate with the external api
 * 
 * @throws InvalidKeyException
 */
private static String getEncodedString(URI fullUrl, String apiKey, String secret, String httpVerb)
			throws InvalidKeyException {
		
	try {
		
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
		SecretKey decryptedSecretKey = 
			keyFactory.generateSecret(new PBEKeySpec(secret.toCharArray()));
	
		// recompose the full url - use only what is required
		String recomposedUrl = fullUrl.getRawPath().replace("/pncp-external-api-rest", "");
	
		if (fullUrl.getRawQuery() != null && !fullUrl.getRawQuery().isEmpty()) {
			recomposedUrl += "?" + fullUrl.getRawQuery();
		}
	
		// build the string to sign
		String stringToSign = String.format("%s %s %s", httpVerb, recomposedUrl, apiKey);
			
		// generate request signature
		Mac mac = Mac.getInstance("hmacSHA256");
		mac.init(decryptedSecretKey);
		byte[] hash = mac.doFinal(stringToSign.getBytes());
		String requestSignature = new String(Base64.encode(hash));
			
		// encode the credentials
		String credentials = apiKey + ":" + requestSignature;
		return new String(Base64.encode(credentials.getBytes()));
			
	} catch (NoSuchAlgorithmException nsaex) {

		nsaex.printStackTrace();
	} catch (InvalidKeySpecException iksex) {

		iksex.printStackTrace();
	}
		
	return null;
}

Sample GET Request

// concatenate api URL together with the resource
URI fullUrl = new URI(apiUrl + resourcePath);

// configure the HTTP call
HttpGet httpget = new HttpGet(fullUrl);
httpget.setHeader("Accept", "application/vnd.pncp.v1.0+json");
httpget.setHeader("Authorization", "PNCP " + getEncodedString(fullUrl, apiKey, secret, "GET"));

// send request to api and obtain the response
	HttpResponse response = new DefaultHttpClient().execute(httpget);
if (response.getStatusLine().getStatusCode() != expectedStatus) {
		throw new Exception(response.toString());
}
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();

// read from response
	String string = getStringFromInputStream(stream);

Sample POST Request

// concatenate api URL together with the resource
URI fullUrl = new URI(externalApiUrl + resourcePath);

// configure the HTTP call
HttpPost httpPost = new HttpPost(fullUrl);
httpPost.setHeader("Accept", "application/vnd.pncp.v1.0+json");
httpPost.setHeader("Authorization", "PNCP " + getEncodedString(fullUrl, apiKey, secret, "POST"));
httpPost.setHeader("Content-Type", "application/vnd.pncp.v1.0+json");

// set all parameters in post request
StringEntity stringEntity = new StringEntity(requestBody);
httpPost.setEntity(stringEntity);

// send request to api and obtain the response
HttpResponse response = new DefaultHttpClient().execute(httpPost);
logger.debug("Response Received!");
if (response.getStatusLine().getStatusCode() != expectedStatus) {
	throw new Exception(response.toString());
}
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();

// read from response
String string = getStringFromInputStream(stream);