karaxuna
karaxuna

Reputation: 26930

Facebook authentication with nodejs

I'm trying to authenticate user in facebook:

var querystring = require('querystring');
var http = require('http');
var fs = require('fs');

getCookies(function(cookies){
  logIn(cookies);
});

function logIn(cookies) {
  var post_data = querystring.stringify({
      'email': 'email@domain',
      'pass': 'password'
  });

  var post_options = {
      host: 'www.facebook.com',
      port: '80',
      path: '/login.php?login_attempt=1',
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': post_data.length,
          'user-agent': 'Mozilla/5.0',
          'set-cookie': cookies[0]
      }
  };

  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      console.log(res.statusCode);
      var data = '';
      res.on('data', function (chunk) {
          data += chunk;
      });
      res.on('end', function () {
          fs.writeFile("C:\\Users\\user\\Desktop\\fb.html", data, function(err) {
              if(err) {
                  console.log(err);
              } else {
                  console.log("The file was saved!");
              }
          });
      });
  });

  post_req.write(post_data);
  post_req.end();
}

function getCookies(callback){
  var get_options = {
      host: 'www.facebook.com',
      port: '80',
      path: '/',
      method: 'GET',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'user-agent': 'Mozilla/5.0'
      }
  };

  var get_req = http.request(get_options, function(res) {
      var cookies = res.headers['set-cookie'];
      res.on('end', function (chunk) {
          callback(cookies);
      });
  });

  get_req.write('');
  get_req.end();
}

But the response is that cookies in my browser are not enabled. Please don't suggest using existing libraries for connecting to facebook, I'm learning... Thanks for help in advance

Upvotes: 0

Views: 506

Answers (1)

Nabs
Nabs

Reputation: 553

Facebook uses OAuth authentication to authenticate an user. I have used oAuth module to get access to the Linkedin APIs at http://nabaruns.blogspot.in/2013/01/linkedin-api-call-using-nodejs-oauth.html. You can try the same and see if you can call graph facebook apis.

Hope this helps

Upvotes: 1

Related Questions