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

我使用Twitter API,试图代表Twitter用户发布推文。

我正在使用node以及passport-twittertwit模块。

一些资源:

成功通过上面的教程进行了护照验证。

我还成功使用twit将其发布到了我的Twitter开发人员帐户。

但是,我很难将这两件事结合起来。尝试以 代表另一个用户 的方式发布到Twitter。为此,我需要获取用户的访问令牌和访问令牌密钥。然后,我需要使用该信息向Twitter API发出发布请求。

我不确定将发帖请求放在护照发件人代码中的位置。我尝试将其放在第二个路由中,这是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('/');
   })

但是我收到一个错误:Invalid or expired token.

我希望它能工作,因为身份验证有效。

这是我第一次使用OAuth,所以也许我误会了所有工作原理。

我应该在哪里发布请求?

更新:

我尝试使用我的开发帐户的访问令牌和密码发布到我的开发帐户。有效。这使我相信用户的访问令牌和密码有问题。

我想我部分知道会发生什么。

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

const access_token_secret = req.query.oauth_verifier;

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

但是现在我想弄清楚访问令牌的秘密在哪里?请求查询对象(req.query)中只有两个属性;

  • oauth_token

  • oauth_verifier

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

lovers_php 回答:使用Node.js,express,twit和passport-twitter代表另一个用户发布到Twitter

我解决了我的问题。一直在passport-twitter的文档中。老兄,我在这个问题上花了几天时间。

  

该策略还需要一个验证回调,该回调将接收访问令牌对应的机密作为参数,以及包含经过身份验证的用户的Twitter个人资料的个人资料。

来自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);
    });
  }
));

我读过这本书,之前也看过。但是假设这是消费者密钥消费者秘密。我没有意识到这就是我要找的东西:访问令牌访问密码

所以您发给twit的帖子会像这样:

passport.use(new Strategy({
  consumerKey: process.env.CONSUMER_KEY,consumerSecret: process.env.CONSUMER_SECRET,callbackURL: 'http://localhost:3000/twitter/return'
},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);
}));

本文链接:https://www.f2er.com/3105432.html

大家都在问