在Android上生成Azure

在Android上生成Azure

本文介绍了在Android上生成Azure SAS令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图生成Azure SAS令牌以便能够使用Service Bus REST API。



找到此链接:



如何在Android上实现同样的功能?



我的当前尝试如下所示:

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

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

尝试{
String stringToSign = new URL(uri).toString()+\\\
+ 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。编码(签名),
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后,我得到以下内容: / b>






更新:找到此链接



在第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);

现在的日期= new 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())+\\\
+ 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());
//然后使用复合签名密钥从签名基本字符串
创建一个oauth_signature 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字符串stringTosign,请试试我的建议,希望这可以给你一些提示。


Trying to generate Azure SAS token in order to be able to use Service Bus REST Api.

Found this link:

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

How to achieve the same on Android?

My Current attempt looks like 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 the Rest API of service bus using Postman i get the following :


Update: Found this link

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

Under section 6 the code for android

解决方案

I have no Android environment to test, I have a similar scenario in only java environment, it works fine, the following is my code:

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

I have changed two places, 1) the tokenExpirationTime 2) URLEncoder.encode the String stringTosign, please try with my suggestion, hope this could give you some tips.

这篇关于在Android上生成Azure SAS令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:58