java - Generate Azure SAS Token on Android -
trying generate azure sas token in order able use service bus rest api.
found link:
http://blog.simontimms.com/2015/01/30/sending-message-to-azure-service-bus-using-rest/
how achieve same on android?
my current attempt looks this:
private string generatesastoken(string uri, string keyname, string key){ string ret = ""; long tokenexpirationtime = (system.currenttimemillis() / 1000) + (10 * 365 * 24 * 60 * 60); try { string stringtosign = new url(uri).tostring() + "\n" + tokenexpirationtime; secretkey secretkey = null; byte[] keybytes = key.getbytes("utf-8"); mac mac = mac.getinstance("hmacsha256"); secretkey = new secretkeyspec(keybytes, mac.getalgorithm()); mac.init(secretkey); string signature = base64.encodetostring(mac.dofinal(stringtosign.getbytes("utf-8")), base64.default); ret = string.format("sharedaccesssignature sr=%s&sig=%s&se=%s&skn=%s", urlencoder.encode(uri), urlencoder.encode(signature), string.valueof(tokenexpirationtime), keyname); } catch (malformedurlexception e) { e.printstacktrace(); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return ret; }
after calling rest api of service bus using postman following :
401 40103: invalid authorization token signature time 261 ms
update: found link
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-get-started/
under section 6 code android
i have no android environment test, have similar scenario in java environment, works fine, following code:
private static string generatesastoken(string uri, string keyname, string key){ string ret = ""; // long tokenexpirationtime = (system.currenttimemillis() / 1000) + (10 * 365 * 24 * 60 * 60); date = new date(); date previousdate=new date(1970); long tokenexpirationtime = ((now.gettime() - previousdate.gettime()) / 1000 )+3600; try { string stringtosign = urlencoder.encode(new url(uri).tostring(),java.nio.charset.standardcharsets.utf_8.tostring()) + "\n" + tokenexpirationtime; system.out.println(stringtosign); secretkey secretkey = null; byte[] keybytes = key.getbytes("utf-8"); mac mac = mac.getinstance("hmacsha256"); secretkey = new secretkeyspec(keybytes, mac.getalgorithm()); mac.init(secretkey); byte[] digest = mac.dofinal(stringtosign.getbytes()); //we use composite signing key create oauth_signature signature base string string signature = base64.encodebase64string(digest); system.out.println( urlencoder.encode(signature, java.nio.charset.standardcharsets.utf_8.tostring())); // string signature = base64.encodebase64string(mac.dofinal(stringtosign.getbytes("utf-8"))); ret = string.format("sharedaccesssignature sr=%s&sig=%s&se=%s&skn=%s", urlencoder.encode(uri, java.nio.charset.standardcharsets.utf_8.tostring()), urlencoder.encode(signature, java.nio.charset.standardcharsets.utf_8.tostring()), string.valueof(tokenexpirationtime), keyname); } catch (malformedurlexception e) { e.printstacktrace(); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return ret; }
i have changed 2 places, 1) tokenexpirationtime 2) urlencoder.encode string stringtosign, please try suggestion, hope give tips.
Comments
Post a Comment