【问题标题】:API Signature GenerationAPI 签名生成
【发布时间】:2014-04-06 22:08:01
【问题描述】:

我正在开发 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:添加秘密并计算 SHA-1 摘要。 Secret 被添加到 SBS 的末尾:

api_format=xml&api_key=XOqEAfxj&api_nonce=80684843&api_timestamp=1237387851&text=d%C3%A9mouA96CFtJa138E2T5GhKfngml


6:为上述字符串计算的 SHA-1 HEX 摘要将是:

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

【问题讨论】:

    标签: javascript node.js api oauth cryptography


    【解决方案1】:

    我相信您实际上可以进入哈希阶段。您正在尝试使用 HMAC 函数,但我认为它们只是使用哈希。试试这个:

    testSignature = crypto.createHash('sha1').update(concat + test.apiSecret).digest('hex');
    

    这应该会得到十六进制字符串fbdee51a45980f9876834dc5ee1ec5e93f67cb89

    【讨论】:

    • 呸哈哈。谢谢@ZachRabbit!
    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2017-03-17
    相关资源
    最近更新 更多