我正在使用node.js request模块从API中获取id_token。在获取该id_token之后,我想发送一个带有set-cookie header to the redirected url的redirect uri响应。但我不太明白该怎么做。
下面是我的代码:
app.use("/nodejs-server/retrieveCode", function(req, res) {
var clientID = 'some random string'
var client_Secret = 'another random string'
var code_token = clientID + ":" + client_Secret
var buffer = new Buffer(code_token)
var token = buffer.toString('base64')
var rtoken = "Basic " + token;
var headers = {
'Content-Type': 'application/json',
'Authorization': rtoken
}
var postData = {grant_type: 'authorization_code', code: req.query.code, redirect_uri: 'http://localhost:3000/nodejs-server/retrieveCode'} //Query string data
var options = {
method: 'POST', //Specify the method
body: postData,
url: 'http://localhost:4000/server/token',
json: true,
headers: headers
}
request(options
, function(error, response, body){
if(error) {
console.log(error);
} else {
//send a redirect uri and set-cookie header response
}
});我试着用
res.redirect(302, "http://localhost:9000");它可以重定向,但我不能将cookie与它一起发送
任何帮助都是非常感谢的。
发布于 2016-06-11 14:28:32
在经历了无数的尝试、错误和谷歌搜索之后,我终于能够实现我的目标。为了将带有过期的cookie发送到重定向URL,我刚刚添加了
const expires = body.exp.toUTCString();
res.cookie('id_token', body.id_token, { expires });
res.redirect(302, 'http://localhost:8080');发布于 2019-04-06 12:06:24
在express 4.16.3中,您必须设置
res.cookie()
在此之前
res.redirect()
它为我工作,没有错误
https://stackoverflow.com/questions/37744629
复制相似问题