在使用Google的oauth2Client.getToken()之后尝试redirect时,无法设置标题。

从谷歌收到oauth2令牌之后,我试图redirect到我的快递服务器上的端点,以提出API请求。

这是authentication请求的stream程:

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... 

任何帮助,非常感谢,我一直在这一个撕掉我的头发了一会儿!

谢谢!