3

Node.js を使用して、Google ブック API とパスポートを使用して Google の認証戦略を使用してブック ライブラリ アプリを作成しようとしています。これまでのところ、認証して API にアクセスできます。ばかげた質問かもしれませんが、データをコレクション ビューで使用できるようにするにはどうすればよいでしょうか。認証後に Google データにアクセスした後、コレクションにリダイレクトする必要がありますが、そのデータを新しいビューに組み込むにはどうすればよいですか?

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) {});
4

1 に答える 1

1

できることは、データをセッション変数に保存してから、別のルートからフェッチすることです。文字列を保存し、別のページからアクセスする例を次に示します。

//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);
});
于 2013-09-04T01:12:59.607 に答える