1

我正在尝试使用twit包为用户个人资料创建推文。这是我正在使用的代码。通过使用此代码,我可以向我的个人资料发送推文,因为我拥有access_tokenaccess_token_secret. 我的问题是如果我想向其他人的个人资料发推文。我无法理解的程序是什么?我正在使用护照库来获取用户详细信息。

var Twit = require('twit');

var T = new Twit({
  consumer_key: '************',
  consumer_secret: '**********',
  access_token: '******************',
  access_token_secret: '******************',
  timeout_ms: 60 * 1000,  
  strictSSL: true,    
})


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

下面是我的护照验证码

'use strict';

require('dotenv').config();

const path = require('path');
const express = require('express');
const passport = require('passport');
const { Strategy } = require('passport-twitter');
const { TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, SESSION_SECRET } =  process.env;
const port = process.env.PORT || 3000;
const app = express();
const routes = require('./routes');

passport.use(new Strategy({
    consumerKey: TWITTER_CONSUMER_KEY,
    consumerSecret: TWITTER_CONSUMER_SECRET,
    callbackURL: '/return'
  },
  (token, tokenSecret, profile, cb) => {
    console.log(token);
    console.log(tokenSecret);
    console.log(profile);
        return cb(null, profile);
}));

passport.serializeUser((user, cb) => {
  cb(null, user);
});

passport.deserializeUser((obj, cb) => {
  cb(null, obj);
});

app.set('view engine', 'ejs');

app.use('/public', express.static(path.join(__dirname, 'public')));

app.use(require('express-session')({ secret: SESSION_SECRET, resave: true, saveUninitialized: true }));

app.use(passport.initialize());
app.use(passport.session());
app.use('/', routes);



app.listen(port);

另一个文件

'use strict';

const express = require('express');
const passport = require('passport');
const router = express.Router();

router.get('/', (req, res, next) => {
    const { user } = req;
    console.log('user details', req.user);
    // res.render('home', { user });
});

router.get('/login/twitter', passport.authenticate('twitter'));

router.get('/logout', (req, res, next) => {
  req.logout();
  res.redirect('/');
});

router.get('/return', 
  passport.authenticate('twitter', { failureRedirect: '/' }),
  (req, res, next) => {
    res.redirect('/');
});

module.exports = router;
4

1 回答 1

0

按照这些步骤得到答案。 https://developer.twitter.com/en/docs/basics/authentication/oauth-1-0a

于 2020-04-20T13:07:20.133 回答