Post 代表另一个用户使用 Node.js、express、twit 和 passport-twitter 到 Twitter
Post To Twitter on Behalf of Another User using Node.js, express, twit, and passport-twitter
使用 Twitter API,我正在尝试 post 代表 Twitter 用户发推文。
我正在使用节点和 passport-twitter and twit 模块。
部分资源:
- 我关注了这个视频教程 NodeJS 和 ExpressJS:使用 Twitter
通过 Passport 进行身份验证。
- 教程的源代码
here。
- Passport-twitter documentation
- twit documentation
我成功按照上面的教程通过 passport-twitter 进行了身份验证。
我也成功post在我的 Twitter 开发者帐户上使用 twit 进行了编辑。
但是,我无法将这两件事结合起来;试图 post 代表另一个用户 发 Twitter 。为此,我需要获取用户的访问令牌和访问令牌机密。然后,我需要使用该信息向 Twitter API 发出 post 请求。
我不确定将 post 请求放在 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('/');
})
但是我得到一个错误:Invalid or expired token.
我希望它能工作,因为身份验证有效。
这是我第一次使用 OAuth,所以我可能误解了这一切的工作原理。
我应该把 post 请求放在哪里?
更新:
我尝试 post 访问我的开发帐户,使用我的开发帐户的访问令牌和密码。有效。这让我相信用户的访问令牌和秘密有问题。
我想我知道部分是怎么回事。
我假设在请求查询对象中找到的 属性 oauth_verifier
是访问令牌秘密。
const access_token_secret = req.query.oauth_verifier;
但是现在我不认为oauth_verifier
和access token secret是一样的。 oauth_verifier
的字符数少于我的开发帐户的访问令牌密钥。所以看起来数据类型不同。
但现在我想弄清楚访问令牌的秘密在哪里? request查询对象中只有两个属性(req.query
);
oauth_token
oauth_verifier
用户的访问令牌秘密在哪里?
我解决了我的问题。它一直在 passport-twitter 的文档中。伙计,我在这个问题上花了几天时间。
The strategy also requires a verify callback, which receives the access token and corresponding secret as arguments, as well as profile which contains the authenticated user's Twitter profile.
在docs中的示例中,您可以在参数中看到token
和tokenSecret
。
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);
});
}
));
我读过这个,以前看过这个。但假设这是 消费者密钥 和 消费者秘密。我没有意识到这正是我要找的东西:访问令牌和访问秘密。
所以你的 post 推特会是这样的:
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);
}));
使用 Twitter API,我正在尝试 post 代表 Twitter 用户发推文。
我正在使用节点和 passport-twitter and twit 模块。
部分资源:
- 我关注了这个视频教程 NodeJS 和 ExpressJS:使用 Twitter 通过 Passport 进行身份验证。
- 教程的源代码 here。
- Passport-twitter documentation
- twit documentation
我成功按照上面的教程通过 passport-twitter 进行了身份验证。
我也成功post在我的 Twitter 开发者帐户上使用 twit 进行了编辑。
但是,我无法将这两件事结合起来;试图 post 代表另一个用户 发 Twitter 。为此,我需要获取用户的访问令牌和访问令牌机密。然后,我需要使用该信息向 Twitter API 发出 post 请求。
我不确定将 post 请求放在 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('/');
})
但是我得到一个错误:Invalid or expired token.
我希望它能工作,因为身份验证有效。
这是我第一次使用 OAuth,所以我可能误解了这一切的工作原理。
我应该把 post 请求放在哪里?
更新:
我尝试 post 访问我的开发帐户,使用我的开发帐户的访问令牌和密码。有效。这让我相信用户的访问令牌和秘密有问题。
我想我知道部分是怎么回事。
我假设在请求查询对象中找到的 属性 oauth_verifier
是访问令牌秘密。
const access_token_secret = req.query.oauth_verifier;
但是现在我不认为oauth_verifier
和access token secret是一样的。 oauth_verifier
的字符数少于我的开发帐户的访问令牌密钥。所以看起来数据类型不同。
但现在我想弄清楚访问令牌的秘密在哪里? request查询对象中只有两个属性(req.query
);
oauth_token
oauth_verifier
用户的访问令牌秘密在哪里?
我解决了我的问题。它一直在 passport-twitter 的文档中。伙计,我在这个问题上花了几天时间。
The strategy also requires a verify callback, which receives the access token and corresponding secret as arguments, as well as profile which contains the authenticated user's Twitter profile.
在docs中的示例中,您可以在参数中看到token
和tokenSecret
。
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);
});
}
));
我读过这个,以前看过这个。但假设这是 消费者密钥 和 消费者秘密。我没有意识到这正是我要找的东西:访问令牌和访问秘密。
所以你的 post 推特会是这样的:
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);
}));