在使用Google的oauth2Client.getToken()后尝试重定向时,无法设置标题

时间:2017-11-27 22:38:25

标签: node.js express oauth-2.0 google-oauth2

从谷歌收到oauth2令牌后,我正在尝试重定向到我的快速服务器上的端点以发出api请求。

这是身份验证请求的流程:

app.get('/api/presentation/getpresentation/:id', function(req, res, next){
  req.session.user = req.user.id;
  req.session.presentation_id = req.params.id;
  res.redirect(google_auth_url)
});

app.get('/oauth2callback', function(req, res) {
  const code = req.query.code;
  oauth2Client.getToken(code, function(err, tokens) {
    req.session.tokens = tokens;
    if (!err) {
      oauth2Client.setCredentials(tokens);
      res.redirect(`/api/presentation/${req.session.presentation_id}`);
      return;
    }
    res.status(500).send(err);
  })  
});

app.get('/api/presentation/:id', **do things with api consent**);

错误:

Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:767:10)
at ServerResponse.location (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:884:15)
at ServerResponse.redirect (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:922:18)
at /Users/joe/school/solo-project/present/server/index.js:112:11
at /Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/auth/oauth2client.js:154:5
at Request._callback (/Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/transporters.js:106:7)
at Request.self.callback (/Users/joe/school/solo-project/present/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1057:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...

非常感谢任何帮助,我已经在这一段时间里撕掉我的头发了一段时间!

谢谢!

0 个答案:

没有答案