【发布时间】:2020-03-09 23:12:58
【问题描述】:
使用 Twitter API,我试图代表 Twitter 用户发布一条推文。
我正在使用节点和 passport-twitter 和 twit 模块。
一些资源:
- 我跟着这个视频教程NodeJS and ExpressJS: Using Twitter authentication via Passport。
- 教程源代码 here。
- Passport-twitter documentation
- twit documentation
我成功按照上面的教程通过 passport-twitter 进行了身份验证。
我还成功在我的 twitter 开发者帐户上使用 twit 发布。
但是,我无法将这两件事结合起来;试图代表另一个用户在 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_tokenoauth_verifier
用户的访问令牌秘密在哪里?
【问题讨论】:
标签: node.js authentication twitter oauth twitter-oauth