如何使用Node.js在Facebook上发帖

How to post on Facebook with Node.js?

本文关键字:Facebook 何使用 Node js      更新时间:2024-04-01

我刚开始摆弄Node.js及其模块导出和passport。在为Facebook设置登录后,我被困了几个小时,无法在应用程序中在墙上发帖。我已经在Facebook开发者上注册了应用程序,以获取它的应用程序ID和应用程序秘密。我还将其注册到localhost。我现在如何在Facebook上发帖?

这是我迄今为止的代码:

var passport = require('passport')
  , FacebookStrategy = require('passport-facebook').Strategy;
passport.use(new FacebookStrategy({
    clientID:   APP_ID,
    clientSecret:   APP_SECRET,
    callbackURL: "http://localhost:3000/"
  },
  function(accessToken, refreshToken, profile, done) {
    /*
    User.findOrCreate(..., function(err, user) {
      if (err) { return done(err); }
      done(null, user);
    });
    */
  }
));

var express = require('express');
var app = express();
app.get('/', function(req, res){
  res.send('hello world <a href="/auth/facebook">Login with Facebook</a>');
});
app.listen(3000);

// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
app.get('/auth/facebook', passport.authenticate('facebook'));
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback', 
  passport.authenticate('facebook', { successRedirect: '/',
                                      failureRedirect: '/login' }
                        )
);

一旦我登录,我的URL看起来像:

http://localhost:3000/?code=AQAbQE...

这个代码变量是令牌,对吧?

我曾经使用过fb模块,它非常容易使用。非常直率。npm安装fb