首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速blox -签名生成问题

快速blox -签名生成问题
EN

Stack Overflow用户
提问于 2016-06-17 17:20:17
回答 1查看 422关注 0票数 1

我试图使用这个链接来获得描述的响应:

代码语言:javascript
复制
{
  "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
  }
}

但现在它说没有找到申请:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>No application found</error>
</errors>

不能继续测试其他请求。这是一个shell脚本,用于获取curl请求:

代码语言:javascript
复制
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.请求体是以排序(按字母顺序排序,而不是以字节的形式排序)通过增加字符串数组‘参数=值’而形成的,以符号"&“分隔。对于作为userid=123传递的参数,只使用这样一行userid=123

另外,我已经准备了一个Swift项目如何生成签名和获取会话,但是仍然有相同的错误,没有找到应用程序。

有什么建议吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-06-29 08:55:16

请验证应用程序ID参数,因为服务器返回:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>No application found</error>
</errors>

例如,生成签名(Java):

代码语言:javascript
复制
    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:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37887048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档