Quantcast
Channel: Service Bus forum
Viewing all articles
Browse latest Browse all 1916

401 - using peek and lock Rest API

$
0
0

I am referring to following link : https://msdn.microsoft.com/en-us/library/azure/hh780722.aspx

I am currently using java with jersey client to using this API but receiving 401. The response entity is empty and i dont know specifically what is wrong.I have tried regenerating the Primary_key from the portal but still see the same issue.

Here is the code snippet

private static final String ENDPOINT = "datamstorequeue/messages/head"; private static final String HOST = "https://<namespace>.servicebus.windows.net/"; @Override public String getMessage() { final ClientConfig readClientConfig = new DefaultClientConfig(); readClientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client readClient = Client.create(readClientConfig); WebResource webResource = readClient.resource(HOST + ENDPOINT); String key = GetSASToken(HOST + ENDPOINT,"All",Constants.QUEUE_PRIMARY_KEY); System.out.println("key:"+ key); ClientResponse response = webResource.header("Authorization", key)

.header("Content-Length", 0).post(ClientResponse.class); if (response.getStatus() != 200) { String output = response.getEntity(String.class); System.out.println("Failed : HTTP error code : "+ response.getStatus() + output); } String output = response.getEntity(String.class); System.out.println("Output : " + output); return output; } private static String GetSASToken(String resourceUri, String keyName, String key) { long epoch = System.currentTimeMillis()/1000L + 60 ; String expiry = Long.toString(epoch); String sasToken = null; try { String stringToSign = URLEncoder.encode(resourceUri, "UTF-8") + "\n" + expiry; String signature = getHMAC256(key, stringToSign); //SharedAccessSignature sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI> sasToken = "SharedAccessSignature sr=" + URLEncoder.encode(resourceUri, "UTF-8") +"&sig=" + URLEncoder.encode(signature, "UTF-8") + "&se=" + expiry + "&skn=All"; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sasToken; } public static String getHMAC256(String key, String input) { byte[] base64key = Base64.decodeBase64(key.getBytes()); Mac sha256_HMAC = null; String hash = null; try { sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(base64key, "HmacSHA256"); sha256_HMAC.init(secret_key); hash = new String(Base64.encodeBase64(sha256_HMAC.doFinal(input.getBytes("UTF-8")))); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return hash; }

I am not sure what can i improve in above code to correct my SAS key.

It looks something like this :

SharedAccessSignature sr=https%3A%2F%2F<namespace>.servicebus.windows.net%2Fdatamstorequeue%2Fmessages%2Fhead&sig=AHyMSNl9tmURr7l0gHzt4rOlS1jjTx9Hi4h13UzcMl4%3D&se=1437443740&skn=All




Viewing all articles
Browse latest Browse all 1916

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>