我正在开发 Node.js 应用程序...尝试从 JWPlatform API 请求数据,但很难创建身份验证签名。也许我的想法完全错误,但我已经联系了 JWPlatform 开发团队,他们说他们对 Node 或 JS 不够熟悉,无法指出我做错了什么......他们都提供 PHP API Kit 和 Python API Kit,我已经看过了,但仍然没有看到我的失误在哪里。
为了让事情变得更简单,我对变量进行了硬编码,并且只是尝试输出与 documented example 中找到的相同的 SHA-1 十六进制摘要。它包含在这篇文章的底部。
记录示例:fbdee51a45980f9876834dc5ee1ec5e93f67cb89
当前输出:f067ff121b5a4388fecc00f01dabeddd02d707f4
霍凯,所以。我认为这些信息已经足够了。
这是我的代码
var crypto = require("crypto");
test = {
apiFormat: 'xml',
apiKey: 'XOqEAfxj',
apiNonce: 80684843,
apiTimestamp: 1237387851,
apiText: ('démo').toString("utf8"),
apiSecret: 'uA96CFtJa138E2T5GhKfngml'
}
// Concatinate string
concat = "api_format=" + test.apiFormat +
"&api_key=" + test.apiKey +
"&api_nonce=" + test.apiNonce +
"&api_timestamp=" + test.apiTimestamp +
"&text=" + encodeURI(test.apiText);
// Create Hash
testSignature = crypto.createHmac("sha1", concat+
test.apiSecret).digest("hex");
console.log(testSignature);
// Outputs: 'f067ff121b5a4388fecc00f01dabeddd02d707f4'
// Expecting: 'fbdee51a45980f9876834dc5ee1ec5e93f67cb89'
他们的documentation :
1:所有文本参数转换为UTF-8编码。
text: démo
api_format: xml
api_key: XOqEAfxj
api_nonce: 80684843
api_timestamp: 1237387851
2:所有文本参数均经过 URL 编码(请参阅 OAuth Core 1.0 第 5.1 节)。
text: d%C3%A9mo
api_format: xml
api_key: XOqEAfxj
api_nonce: 80684843
api_timestamp: 1237387851
3:参数根据其编码名称进行排序(请参阅 OAuth Core 1.0 第 9.1.1 节)。排序顺序是字典字节值排序。
api_format: xml
api_key: XOqEAfxj
api_nonce: 80684843
api_timestamp: 1237387851
text: d%C3%A9mo
4:参数连接在一起形成一个字符串。每个参数的名称与相应的值均由“=”字符分隔(即使该值为空),每个名称-值对由“&”字符分隔(请参阅 OAuth Core 1.0 第 9.1.1 节)。
api_format=xml&api_key=XOqEAfxj&api_nonce=80684843&api_timestamp=1237387851&text=d%C3%A9mo
5:添加 secret 并计算 SHA-1 摘要。 Secret 添加到 SBS 末尾:
api_format=xml&api_key=XOqEAfxj&api_nonce=80684843&api_timestamp=1237387851&text=d%C3%A9mouA96CFtJa138E2T5GhKfngml
6:计算出的上述字符串的 SHA-1 十六进制摘要将为:
fbdee51a45980f9876834dc5ee1ec5e93f67cb89
经过身份验证的 API 调用将如下所示:
http://api.jwplatform.com/v1/videos/list?text=d%C3%A9mo&api_nonce=80684843&api_timestamp=1237387851&api_format=xml&api_signature=fbdee51a45980f9876834dc5ee1ec5e93f67cb89&api_key=XOqEAfxj
最佳答案
我相信您实际上已经可以完成哈希阶段了。您正在尝试使用 HMAC 函数,但我认为它们只是使用哈希。试试这个:
testSignature = crypto.createHash('sha1').update(concat + test.apiSecret).digest('hex');
这应该会得到十六进制字符串fbdee51a45980f9876834dc5ee1ec5e93f67cb89
。
关于javascript - API签名生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22187779/