【问题标题】:Coinkite, Node.js authorization is failing, not sure whyCoinkite,Node.js 授权失败,不知道为什么
【发布时间】:2015-06-23 20:56:41
【问题描述】:

当我使用 api ket off http://forecast.io/ 尝试下面的代码时,它起作用了。但是当我通过https://coinkite.com/ 尝试它时,我得到了一个 401 未经授权的异常。

我不明白为什么它没有通过。我有一个单独的项目文件,它运行类似的 coinkite 代码,但它是一个命令行应用程序,它给了我正确的响应。

这是执行的重点,

/*global require*/
/*global console*/
/*global process*/
/*jslint nomen: true*/
/*global __dirname*/
/*jslint nomen: false*/

// Dependancies
var bodyParser = require('body-parser');
var express = require('express');
var path = require('path');

// Express
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var currentPort = process.env.PORT || 3000;
app.set('port', currentPort);

   // Here you set that all templates are located in `/views` directory
   /*jslint nomen: true*/
   app.set('views', path.join(__dirname, 'views'));
   /*jslint nomen: false*/
   app.set('view engine', 'ejs');

   // Routes
   var coinkite = require('./routes/coinkite');

   app.use('/coinkite', coinkite);

   // Start Server
   app.listen(currentPort);
   console.log('Server is running at port: ' + currentPort);

这是硬币的路线,

/*global require*/
/*global console*/
/*global module*/
/*global process*/
/*global CK_API */

// Dependancies

var express = require('express');
var crypto = require('crypto');
var request = require('request');

var CK_API_KEY = 'XX-XX-XXXX';
var CK_API_SECRET = 'XXXXXXXXXXX';
var CK_ENDPOINT = '/v1/my/self';
var CK_DATA = '';

var router_CK = express.Router();

router_CK.get('/', function (req, res) {
    'use strict';

    var ts = (new Date()).toISOString(),
        sign = CK_ENDPOINT + '|' + ts,
        hm = crypto.createHmac('sha256', CK_API_KEY).update(sign).digest('hex'),
    options = { uri: 'https://api.coinkite.com' + CK_ENDPOINT, headers: { 'X-CK-Key': CK_API_KEY, 'X-CK-Sign': hm, 'X-CK-Timestamp': ts }, json: true};

    request.get(options, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            console.log(body);
            CK_DATA = JSON.parse(body);
            console.log(CK_DATA);
        }
    });

    res.render('coinkite-index', {data: CK_DATA});
});

// Return Router
module.exports = router_CK;

顺便说一句,如果我想在 Node.js 中的模块之间传递一个变量,我该怎么做?

感谢您的帮助。

干杯

【问题讨论】:

    标签: javascript node.js asp.net-web-api


    【解决方案1】:

    你的代码看起来和官方的JS代码from Coinkite github非常相似。

    您收到 401 响应的什么错误消息?这通常非常详细且很有帮助。


    稍后

    再次查看您的代码,我现在看到了问题...您有:

        hm = crypto.createHmac('sha256', CK_API_KEY).update(sign).digest('hex'),
    

    但是在那一行,它应该是在 HMAC 中使用的 CK_API_SECRET(不是 KEY)。

    【讨论】:

    • 非常相似,只是在玩几个不同的 node.js 模块来完成工作。它只是说“服务不可用”
    【解决方案2】:

    代码有效,必须传递 CK_API_SECRET 而不是 CK_API_KEY 的标志。

    hm = crypto.createHmac('sha256', CK_API_KEY).update(sign).digest('hex'),
    

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2014-04-08
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多