更新:以下来自 Remus 的反馈。
我可以成功授权我的 Web 应用程序并取回一个access_token
和refresh_token
. 我正在使用不错的Grant NPM(或者这真的是 grant-express?)来获得身份验证(感谢作者Simeon Valichkov)。
如何使用access_token
NPMjawbone-up
或Purest
问题#1 - 使用 a 创建此 API 调用express-bearer-token
并实际取回我的 Jawbone json 数据的最简单方法是什么?
我在页面上看到的是令牌(一个 looong 字符串)而不是 Jawbone json 结果数据。
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');
});
我的 Grant 配置文件看起来像这样,并且似乎是存储我的令牌的明显位置。
module.exports = {
"server": {
"protocol" : "https",
"host" : "localhost:5000"
},
'jawbone' : {
'key' : '6f**********',
'secret' : '9b918*********************',
'callback' : '/sleeps',
'scope' : ['basic_read','extended_read','move_read','sleep_read']
}
};