2016-06-17 15 views
1

我试图用这个link获得描述回应:快速blox公司 - 签名生成问题

{ 
    "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> 

不能继续进行测试另一个请求。这是一个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 

因此,有一些信息regrding这也许是我已经创建shell脚本错误的方式:

身体的HMAC-SHA功能的请求,并带有密钥auth_secret。 通过增加用符号“&”分隔的字符串数组'参数=值', 将请求正文形成为排序(按字母顺序排序,如 符号,而不是字节)。对于作为 用户[ID]传递的参数= 123使用的正是这样一条线的用户[ID] = 123

而且我已经坦然一个Swift project如何生成签名,并得到会议,但仍然有没有找到应用程序的同一个错误。

任何推荐?由于

回答

0

请验证应用程序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; 
    } 
+0

感谢安德烈我将检查和让你知道,真的很感谢你的帮助! –

+0

@Matrosov亚历山大,欢迎您! –

+0

嘿安德烈,我试图使用你的建议,我建立了Swift项目https://github.com/matrosovDev/QuickBloxApiTest/tree/master/QuickBloxTest我试图做同样的事情,但仍然得到“未找到应用程序”,我重新检查了应用程序ID,我完全确定这是正确的,因为我使用网站上的按钮复制了它。在我的例子中,我在Consts.swift中留下空的字段,因为我不想分享我的。但你可以使用你的任何应用程序来检查它。再次感谢您的帮助! –