使用Node.js、express、twit和passport-twitter代表另一个用户发布Twitter帖子

3
使用Twitter API,我正在尝试代表Twitter用户发布推文。我正在使用node和passport-twittertwit模块。一些资源:我遵循了这个视频教程NodeJS和ExpressJS:使用Passport进行Twitter身份验证。教程的源代码在这里Passport-twitter文档twit文档。我已经成功地按照上面的教程通过passport-twitter进行了身份验证。
我还成功使用twit在我的twitter开发者账户上发布了帖子。然而,我在尝试代表另一个用户发布推文时遇到了问题。我需要获取该用户的访问令牌和访问令牌密钥,然后使用这些信息向Twitter API发送一个帖子请求。我不确定在passport-twitter代码中应该放置这个帖子请求。我尝试将其放在第二个路由中,即Twitter在用户登录后重定向到的URL中。
   app.get('/twitter/login', passport.authenticate('twitter'))

   app.get('/twitter/return', passport.authenticate('twitter', {
       failureRedirect: '/'
   }), function(req, res) {
     //Post using twit
     //grab access token and access token secret from the request query
       const access_token = req.query.oauth_token;
       const access_token_secret = req.query.oauth_verifier;

       //set the configurations with the access token and access token secret that we just got
       const config = {
         consumer_key:         <consumer key here>,
         consumer_secret:      <consumer secret here>,
         access_token, 
         access_token_secret,
         timeout_ms:           60*1000,  
         strictSSL:            true
       }

       //pass in the configurations
       var T = new Twit(config);

       //post
       T.post('statuses/update', { status: 'hello world!' }, function(err, data, response) {
         if (err)console.log("oops, didn't tweet: ", err.message);
       })

       res.redirect('/');
   })

但是我遇到了一个错误:无效或过期的令牌。

我原本以为会成功,因为已经进行了身份验证。

这是我第一次使用OAuth,也许我对它的工作方式有所误解。

我应该把这个请求放在哪里?

更新:

我尝试使用我的开发者帐户、开发者帐户的访问令牌和密钥进行发布。它起作用了。这让我相信用户的访问令牌和密钥存在问题。

我想我知道部分情况了。

我假设在请求查询对象中找到的oauth_verifier属性是访问令牌密钥。

const access_token_secret = req.query.oauth_verifier;

但是现在我不认为oauth_verifier与访问令牌密钥相同。oauth_verifier的字符数比我的开发帐户的访问令牌密钥少。因此,数据类型似乎不同。

但现在我正在尝试找出用户的访问令牌密钥在哪里?请求查询对象 (req.query) 中只有两个属性:

  • oauth_token

  • oauth_verifier

用户的访问令牌密钥在哪里?

1个回答

5
我解决了我的问题。一直都在passport-twitter的文档中。天啊,我为此问题花了好几天。
这个策略还需要一个验证回调函数,它接收访问令牌相应的密钥作为参数,以及包含经过身份验证的用户的 Twitter 个人资料的 profile。
-来自passport-twitter readMe的例子
docs的示例中,您可以在参数中看到tokentokenSecret
passport.use(new TwitterStrategy({
  consumerKey: TWITTER_CONSUMER_KEY,
  consumerSecret: TWITTER_CONSUMER_SECRET,
  callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
  },
  function(token, tokenSecret, profile, cb) {
    User.findOrCreate({ twitterId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

我曾经读过这个并且之前也看到过。但是我认为那是消费者密钥消费者密钥,我没有意识到那就是我要找的:访问令牌访问密钥
所以你发布推文的内容应该是这样的:
passport.use(new Strategy({
  consumerKey: process.env.CONSUMER_KEY,
  consumerSecret: process.env.CONSUMER_SECRET,
  callbackURL: 'http://localhost:3000/twitter/return'
}, function(token, tokenSecret, profile, callback) {
  const configs = createConfigs(token, tokenSecret);


  // Post to twitter

  var Twit = require('twit')

  var T = new Twit({
    consumer_key:         '...', //get this from developer.twitter.com where your app info is
    consumer_secret:      '...', //get this from developer.twitter.com where your app info is
    access_token:         token,
    access_token_secret:  tokenSecret,
    timeout_ms:           60*1000,  // optional HTTP request timeout to apply to all requests.
    strictSSL:            true,     // optional - requires SSL certificates to be valid.
  })

  //
  //  tweet 'hello world!'
  //
  T.post('statuses/update', { status: 'hello world!' }, function(err, 
  data, response) {
    console.log(data)
  })


  return callback(null, profile);
}));


嗨@MuhanadY,我不太明白你在问什么。 - Dashiell Rose Bark-Huss
1
非常感谢您的回复。我已经解决了这个问题,忘记删除注释了。对此我很抱歉。 - MuhanadY

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接