【问题标题】:cex.io web socket Authentication timestamp errorcex.io web socket身份验证时间戳错误
【发布时间】:2017-07-09 17:22:36
【问题描述】:

我目前正在尝试连接到 CEX.IO 比特币交易所的 websocket。 Websocket 连接正常,但在身份验证时,出现错误:时间戳不在 20 秒范围内。我不知道这是什么错误。

createSignature 的测试用例 1 和 2 正常 (https://cex.io/websocket-api#authentication)。

计算签名和请求参数的代码

const WebSocket = require('ws');
const cexioWs = new WebSocket(
    'wss://ws.cex.io/ws/',
    {
        perMessageDeflate: false
    }
);
function createAuthRequest(apiKey, apiSecret) {
    let curTime = Math.floor(Date.now() / 1000);
    let hmac = crypto.createHmac('sha256', apiSecret);
    hmac.update(curTime.toString());
    hmac.update(apiKey);
    let args =
        {
            e: "auth",
            auth: {
                key: apiKey,
                signature: hmac.digest('hex'), //createSignature(curTime, apiKey, apiSecret),
                timestamp: curTime
            }
        };
    let authMessage = JSON.stringify(args);
    console.log(args);
    return authMessage;
}
cexioWs.on('message', (mess, error) => {
    //console.log("connected");
    console.log("cexio message");
    console.log(mess);
    let JSONMess = JSON.parse(mess);
    if (JSONMess.e === "connected") {
        cexioWs.send(createAuthRequest(key, secret));
        cexioWs.send(JSON.stringify({
            e: "subscribe",
            roomss: [
                "tickers"
            ]
        }));
    }
    if (JSONMess.e === "ping") {
        console.log("pong message");
        cexioWs.send(JSON.stringify({e: "pong"}));
    }
});

【问题讨论】:

    标签: javascript node.js websocket


    【解决方案1】:

    这是工作代码:

    const crypto = require('crypto')
    const WebSocket = require('ws')
    
    var apiKey = ''
    var apiSecret = ''
    
    const cexioWs = new WebSocket('wss://ws.cex.io/ws/', {perMessageDeflate: false });
    
    function createSignature(timestamp, apiKey, apiSecret){ 
        var hmac = crypto.createHmac('sha256', apiSecret );
        hmac.update( timestamp + apiKey );
        return hmac.digest('hex');
    }
    
    function createAuthRequest(apiKey, apiSecret ){
        var timestamp = Math.floor(Date.now() / 1000);
        var args = { e: 'auth', auth: { key: apiKey,
            signature: createSignature(timestamp, apiKey, apiSecret), timestamp: timestamp } };
        var authMessage = JSON.stringify( args );
        return authMessage;
    }
    
    cexioWs.on('message', (mess, error) => {
            console.log("cexio message");
            console.log(mess);
        let JSONMess = JSON.parse(mess);
        if (JSONMess.e === "connected") {
            cexioWs.send(createAuthRequest(apiKey, apiSecret));
            cexioWs.send(JSON.stringify({
                e: "subscribe",
                rooms: [
                    "tickers"
                ]
            }));
        }
        if (JSONMess.e === "ping") {
            console.log("pong message");
            cexioWs.send(JSON.stringify({e: "pong"}));
        }
    });
    

    【讨论】:

    • 此代码也不起作用。给出错误消息 {"e":"auth","data":{"error":"Timestamp is not in 20sec range"},"ok" :"错误"}
    • 欧盟:维尔纽斯 UTC+02:00
    【解决方案2】:

    不知道这是否有帮助,但我两天都遇到同样的问题,检查了所有内容,然后我检查了代码看起来非常好。后来我检查了我得到的实际时间,并将其与互联网时间进行了比较。我的电脑时间比 Internet 时间早 4 分钟,我的设置已关闭“从 Internet 更新时间”。

    将我的计算机时间与 Internet 同步后,我运行了脚本,它运行良好。

    故事的寓意,确保您的 PC 时间和 Internet 时间相同。 祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多