如何在路由之间发送访问令牌

时间:2019-06-06 09:21:31

标签: node.js express

因此,我在节点中使用spotify api获得了授权。我可以请求接收我的帐户数据。我需要发送请求以获取其他数据,但是我仅在一个页面上拥有访问令牌,而无法将其发送到其他页面。有办法解决问题吗?

request.post(authOptions, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      const access_token = body.access_token;
      const refresh_token = body.refresh_token;

      const options = {
        url: "https://api.spotify.com/v1/me",
        headers: { Authorization: `Bearer ${access_token}` },
        json: true
      };

      request.get(options, (error, response, body) => {
        console.log(body);
      });

      res.redirect(
        url.format({
          pathname: "/homepage",
          query: {
            access_token: access_token,
            refresh_token: refresh_token
          }
        })
      );
router.get("/homepage", (req, res) => {
  fetch("https://api.spotify.com/v1/me", {
    headers: {
      Authorization: `Bearer ${req.query.access_token}` 
      //I need this access token on another page
    }
  })
    .then(response => {
      return response.json();
    })
    .then(data => {
      res.render("index/homepage", {
        data: data
      });
    });
});

router.get("/library", (req, res) => {
  // New Releases
  fetch("https://api.spotify.com/v1/browse/new-releases", {
    headers: {
      Authorization: `Bearer ${req.query.access_token}`
    }
  })
    .then(res => res.json())
    .then(data => {
      console.log(data);
      res.render("index/library", {
        item: data
      });
    })
    .catch(err => console.log(err));
});

0 个答案:

没有答案