代码之家  ›  专栏  ›  技术社区  ›  Shubham Khatri

将setcookie头发送到node.js中的重定向url

  •  26
  • Shubham Khatri  · 技术社区  · 10 年前

    我正在使用node.js request 模块从API获取id_token。获取该id_token后,我想发送一个 redirect uri 用一个 set-cookie header to the redirected url .但我不太明白怎么做。

    这是我的代码:

    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与它一起发送

    感谢任何帮助。

    2 回复  |  直到 8 年前
        1
  •  40
  •   rofrol    4 年前

    经过多次尝试、错误和谷歌搜索,我终于实现了我的目标。为了将过期的cookie发送到重定向URL,我添加了

    const expires = body.exp.toUTCString();
    res.cookie('id_token', body.id_token, { expires });
    res.redirect(302, 'http://localhost:8080');
    
        2
  •  1
  •   Rizky Susanto    7 年前

    在表达式4.16.3中,必须设置

    res.cookie()

    之前

    资源重定向()

    它对我很有效,没有错误

    推荐文章