带有node.js和jawbone-up NPM的Oauth access_token

时间:2015-07-22 20:20:10

标签: node.js api oauth-2.0 access-token jawbone

更新:遵循以下Remus的反馈。

我可以成功授权我的网络应用程序并返回function getRandomItems(cb) { return $http.get('/api/smartTableServer', {cache: 'true'}); } function getPage(start, number, params) { getRandomItems.success(function(randomItems) { // do what you want to do with randomItems }; ) access_token。我正在使用优秀的Grant NPM (or is that really grant-express?)进行身份验证(感谢作者Simeon Valichkov)。

如何使用refresh_tokenaccess_token等NPM将jawbone-up传递给我的Jawbone API调用作为持票人令牌?

问题#1 - 使用Purest创建此API调用的最简单方法是什么,并实际取回我的Jawbone json数据?

我在页面上看到的是令牌(一​​个looong字符串),而不是Jawbone json结果数据。

express-bearer-token

我的Grant配置文件看起来像这样,似乎是存储我的令牌的明显位置。

var express =   require('express')
  , session =   require('express-session')
  , ejs     =   require('ejs')
  , app     =   express()
  , fs      =   require('fs')
  , https   =   require('https')
  , Grant   =   require('grant-express')
  , grant   =   new Grant(require('./config'))
  , bodyParser = require('body-parser')
  , Purest  =   require('purest')
  , jawbone =   new Purest({provider: 'jawbone'})
  , morgan  =   require('morgan')
  , bearerToken = require('express-bearer-token');

    app.set('view engine', 'ejs');
    app.use(bodyParser.urlencoded({extended:true}))
    app.use(session({secret:'grant'}))
    app.use(grant)
    app.use(morgan('combined'))
    app.use(bearerToken());
    app.use(function (req, res) {
        res.send('Token '+req.token);
    });

var $today      = new Date()
var $start      = new Date($today); $start.setDate($today.getDate() -7)
var $end        = new Date($today)
var $startDate  = Math.floor(($start).getTime()/1000)
var $endDate    = Math.floor(($end).getTime()/1000)


    app.get('/sleeps', function (req, res) {

        //res.send(JSON.stringify(req.query.raw, null, 2))

        jawbone.query()
            .select('sleeps')
            .where ({start_date:$startDate, end_date:$endDate})
            .auth(req.token)
            .request(function(err, res, body) {
              // expecting (hoping) to get sleep json here ...??
                var result = JSON.parse(body);
                res.json(result.data.items)
            })
    }); 

// HTTPS
var sslOptions = {
        key     : fs.readFileSync('./.server.key'),
        cert    : fs.readFileSync('./.server.crt')
    };
var secureServer = https.createServer(sslOptions, app).listen(5000, function(){
    console.log('Listening on 5000');
});

1 个答案:

答案 0 :(得分:2)

只是为了澄清一下 - 你问的是如何获取用户向服务器发出请求时使用的令牌?

就我个人而言,我已经采取了多种方式,特别是使用正则表达式从标题中抓取Authorization: Bearer <token>。但最后,我发现使用Express时我的首选解决方案是使用express-bearer-token中间件:

express = require('express');
bearerToken = require('express-bearer-token');
app = express();

app.use(bearerToken());
app.use(function (req, res) {
    res.send('Token '+req.token);
});

因此,在您的情况下,它将如此简单:

app.get('/sleeps', function(req, res) {
    jawbone.query()
        .select('sleeps')
        .where ({start_date:'', end_date:''})
        .auth(req.token)
        .request(function(err, res, body) {
            res.json(req.query.raw);
        })
});