Quick blox - 签名生成问题

时间:2016-06-17 17:20:17

标签: ios quickblox

我正在尝试使用此link来获得描述的响应:

{
  "session": {
    "application_id": 2,
    "created_at": "2012-04-03T07:34:48Z",
    "device_id": null,
    "id": 743,
    "nonce": 1308205278,
    "token": "0e7bc95d85c0eb2bf052be3d29d3df523081e87f",
    "ts": 1333438438,
    "updated_at": "2012-04-03T07:34:48Z",
    "user_id": null
  }
}

但现在它说应用程序未找到:

<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>No application found</error>
</errors>

无法继续测试其他请求。这是我用来获取curl请求的shell脚本:

timestamp=`date +%s`

body="application_id=HIDDENAPPLICATIONIDHERE&auth_key=HIDDENAUTHKEYHERE&nonce=2342546&timestamp=$timestamp"

signature=`echo -n $body | openssl sha -hmac HIDDENSECRETHERE`

body=$body"&signature="$signature

#echo $body
#echo $signature

#exit 0

curl -X POST \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-d $body \
https://api.quickblox.com/session.xml

所以有一些信息重新说明这可能是我以错误的方式创建了shell脚本:

  

请求正文的HMAC-SHA函数,带有密钥auth_secret。   请求体形成为排序(按字母顺序排序,如   增加字符串数组'parameter = value',符号,而不是字节),   用符号“&amp;”分隔。对于作为a传递的参数   user [id] = 123仅用于用户[id] = 123

这样的行

此外,我已准备好Swift project如何生成签名和获取会话,但在找不到应用程序时仍然存在相同的错误。

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:1)

请验证Application ID参数,因为服务器返回:

<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>No application found</error>
</errors>

例如,生成签名(Java):

    Random random = new Random();

    String nonce = Integer.toString(random.nextInt());
    long time = System.currentTimeMillis() / 1000;
    String timestamp = Long.toString(time);
    String signature;

    String str = "application_id=" + applicationId + "&" + "auth_key=" + authKey + "&" + "nonce="
            + nonce + "&" + "timestamp=" + timestamp + "&" + "user[login]=" + adminLogin + "&" + "user[password]="
            + adminPassword;

    signature = UtilsMethods.calculateHMAC_SHA(str, authSecret);

calculateHMAC_SHA:

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    public static String calculateHMAC_SHA(String data, String key) throws SignatureException {
        String result = null;
        try {

            // get an hmac_sha1 key from the raw key bytes
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

            // get an hmac_sha1 Mac instance and initialize with the signing key
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(signingKey);

            byte[] digest = mac.doFinal(data.getBytes());

            StringBuilder sb = new StringBuilder(digest.length * 2);
            String s;
            for (byte b : digest) {
                s = Integer.toHexString(0xFF & b);
                if (s.length() == 1) {
                    sb.append('0');
                }

                sb.append(s);
            }

            result = sb.toString();

        } catch (Exception e) {
            throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
        }

        return result;
    }