AWS lambda在Twitter授权后重新路由

发布于 2025-02-06 14:42:41 字数 1346 浏览 3 评论 0原文

我实现了Twitter登录授权器,然后将API路由作为回调。

在该路线上引起的lambda函数如下:

const loginTwitterCallback = async (e, context) => {
  const fetch = (...args) =>
    import("node-fetch").then(({ default: fetch }) => fetch(...args));

  const state = e.queryStringParameters.state;
  const code = e.queryStringParameters.code;

  try {
    await fetch(
      "https://api.twitter.com/2/oauth2/token?code=" +
        code +
        "&grant_type=authorization_code&client_id=" +
        process.env.TWITTER_CLIENT_ID +
        "&code_verifier=jwqoijoiw&redirect_uri=" + MY REDIRECT URI,
      {
        method: "POST",
        headers: {
          "Content-type": "application/x-www-form-urlencoded",
        },
      }
    )
      .then((res) => {
        return res.json();
      })
      .then(async (data) => {
        const accessToken = data.access_token;
        return {
          headers: {
            Location:
              "http://127.0.0.1:3000/auth/social?type=twitter&access_token=" +
              encodeURIComponent(accessToken),
          },
          body: null,
          statusCode: 302,
        };
      });
  } catch (err) {
    console.log(err);
  }
};

基本上应将用户重新路由到前端,在此,将向API提出另一个POST请求,该请求将带有载体令牌向Twitter API提出请求并更新数据库。

关键是,首先,我并没有将其重定向到前端,我不明白如何修复它。

非常感谢。

I implemented a twitter login authorizer and I put an API route as the callback.

The Lambda function evoked on that route is the following:

const loginTwitterCallback = async (e, context) => {
  const fetch = (...args) =>
    import("node-fetch").then(({ default: fetch }) => fetch(...args));

  const state = e.queryStringParameters.state;
  const code = e.queryStringParameters.code;

  try {
    await fetch(
      "https://api.twitter.com/2/oauth2/token?code=" +
        code +
        "&grant_type=authorization_code&client_id=" +
        process.env.TWITTER_CLIENT_ID +
        "&code_verifier=jwqoijoiw&redirect_uri=" + MY REDIRECT URI,
      {
        method: "POST",
        headers: {
          "Content-type": "application/x-www-form-urlencoded",
        },
      }
    )
      .then((res) => {
        return res.json();
      })
      .then(async (data) => {
        const accessToken = data.access_token;
        return {
          headers: {
            Location:
              "http://127.0.0.1:3000/auth/social?type=twitter&access_token=" +
              encodeURIComponent(accessToken),
          },
          body: null,
          statusCode: 302,
        };
      });
  } catch (err) {
    console.log(err);
  }
};

Basically the user should be re-routed to the front-end where another POST request will be made to the API which will make a request to the Twitter API with the Bearer token and update the database.

The point is, I'm not being redirected to the front-end in the first place and I don't understand how to fix it.

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文