我正在与Node.js合作,尝试使用Google图书API和带有Google策略的passport来制作图书库应用程序。到目前为止,我可以验证和访问API。现在可能是一个愚蠢的问题,如何将数据重新用于我的集合视图?身份验证后,我可以访问谷歌数据,然后我需要重定向到集合,但我如何将这些数据构建到我的新视图中?
app.get('/auth/google', passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/books', 'https://www.googleapis.com/auth/userinfo.profile']
}));
app.get('/auth/google/callback', passport.authenticate('google', {failureRedirect: '/'}), function (req, res) {
// Successful authentication, get data and redirect to user collection
googleapis.discover('books', 'v1').execute(function (err, client) {
oath2Client.credentials = {
access_token: req.user.accessToken,
refresh_token: req.user.refreshToken
};
var req1 = client.books.mylibrary.bookshelves.list().withAuthClient(oath2Client);
req1.execute(function (err, bookshelves) {});
});
res.redirect('/collection');
});
app.get('/collection', routes.collection, function (req, res) {});
您可以将数据存储在会话变量中,然后从另一个路由获取数据。下面是一个存储字符串并从另一个页面访问它的示例:
//enable session support
app.use(express.cookieParser());
app.use(express.session({
secret: 'secret key for signed cookies'
}));
app.get('/foo', function(req, res) {
req.session.property = 'property value';
res.redirect('/bar');
});
app.get('/bar', function(req, res) {
//the session variables can be accessed here too
res.send(req.session.property);
});