gpt4 book ai didi

java - 在 Android 上生成 Azure SAS token

转载 作者:行者123 更新时间:2023-12-01 11:21:26 26 4
gpt4 key购买 nike

尝试生成 Azure SAS token 以便能够使用服务总线 REST Api。

找到此链接:

http://blog.simontimms.com/2015/01/30/sending-message-to-azure-service-bus-using-rest/

如何在 Android 上实现相同的效果?

我当前的尝试如下所示:

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;
}

使用 Postman 调用服务总线的 Rest API 后,我得到以下信息:

401 40103: Invalid authorization token signature Time 261 ms

<小时/>

更新:找到此链接

https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-get-started/

第 6 节下的 android 代码

最佳答案

我没有Android环境可以测试,我在只有java环境下有类似的场景,它工作正常,以下是我的代码:

private static String generateSasToken(String uri, String keyName, String key){
String ret = "";

// long tokenExpirationTime = (System.currentTimeMillis() / 1000) + (10 * 365 * 24 * 60 * 60);

Date now = 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 then use the composite signing key to create an oauth_signature from the 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;
}

我改变了两个地方,1)tokenExpirationTime 2)URLEncoder.encode the String stringTosign,请尝试我的建议,希望这能给你一些提示。

关于java - 在 Android 上生成 Azure SAS token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com