使用Node.js、express、twit和passport Twitter代表其他用户发布到Twitter
使用twitterapi,我试图代表Twitter用户发布一条tweet 我正在使用node和and模块 一些资源:使用Node.js、express、twit和passport Twitter代表其他用户发布到Twitter,node.js,authentication,twitter,oauth,twitter-oauth,Node.js,Authentication,Twitter,Oauth,Twitter Oauth,使用twitterapi,我试图代表Twitter用户发布一条tweet 我正在使用node和and模块 一些资源: 我遵循了这个视频教程 教程的源代码 . 按照上面的教程,我成功地通过了passport twitter的身份验证 我还成功地使用twit在我的twitter开发者帐户上发布了帖子 然而,我很难将这两件事结合起来;试图代表另一个用户在twitter上发帖。为此,我需要获取用户的访问令牌和访问令牌机密。然后,我需要用这些信息向twitterapi发出post请求 我不确定在
- 我遵循了这个视频教程李>
- 教程的源代码 . 李>
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\u验证器与访问令牌秘密相同<代码>oauth_验证程序的字符数少于我的开发人员帐户的访问令牌密码。所以看起来数据类型是不同的
但现在我想弄清楚访问令牌的秘密在哪里?请求查询对象中只有两个属性(req.query
)
oauth\u令牌
oauth\u验证器
用户的访问令牌密码在哪里?我解决了我的问题。它一直都在文档中。伙计,我在这个问题上花了好几天时间 该策略还需要验证回调,它接收访问令牌和相应的机密作为参数,以及包含已验证用户的Twitter配置文件的配置文件 -从 在中的示例中,您可以在参数中看到
token
和tokenSecret
passport.use(新推特策略)({
consumerKey:TWITTER_CONSUMER_KEY,
消费者信任:推特(TWITTER)消费者(CONSUMER)秘密,,
回调URL:“http://127.0.0.1:3000/auth/twitter/callback"
},
功能(令牌、令牌密码、配置文件、cb){
findOrCreate({twitterId:profile.id},函数(err,User){
返回cb(错误,用户);
});
}
));
我以前读过这本书,也看过这本书。但假设这是消费者密钥和消费者秘密。我没有意识到这正是我想要的:访问令牌和访问秘密
所以你在twit上的帖子是这样的:
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我很难理解您的问题非常感谢您的回复。我确实解决了这个问题,并放弃删除评论。对不起
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);
}));