首页 > 解决方案 > Angular 和 Node js Twitter 登录

问题描述

我正在尝试使用 Angular 和节点 js 进行 twitter 身份验证,但它不起作用。我有这个角度:

   signInWithTwitter() {
     this.http.get(`http://localhost:3000/twitter`, { withCredentials: true }).subscribe((results) => 
    {
     // The url is -> https://api.twitter.com/oauth/authenticate?oauth_token=xxxx
      window.location.href = results["url"];
    }); 
  }

我正在发送 window.location.href 因为我无法从 angular 调用任何外部链接,并且在节点 js 上我有:

var _requestSecret;

app.get("/twitter", (req, res) => {
   tw.login((err, tokenSecret, url) => {
    if (err) {
    // Handle the error your way
    }

   // Save the OAuth token secret for use in your /twitter/callback route
   _requestSecret = tokenSecret;

   res.json({ url: url });


 });
});

节点js上的回调函数是:

app.get("/twitter/callback", (req, res) => { tw.callback({
      oauth_token: req.query.oauth_token,
      oauth_verifier: req.query.oauth_verifier,
    },
    _requestSecret,
    (err, user) => {
    if (err) {
      // Handle the error your way
    }

    // Redirect to whatever route that can handle your new Twitter login user details!
    res.redirect("http://localhost:4200/");
  }
);
});

我在 twitter/callback 上有用户,但我需要一种将用户发送到 Angular 的方法。

标签: node.jsangularauthenticationtwitter

解决方案


推荐阅读