/ Nodejs

NodeJS: Get Facebook User Profile info from ID

Create the Facebook Application

Create a Facebook Application using the Facebook Developer Tool.

After creating the app go to the application settings and get the values of the parameters saying App ID and App Secret, we will have to use them later.

Authenticate Your Application

As Facebook Graph API uses OAuth for authorization, so we need to authenticate the application for the permissions of some Graph API calls we gonna use. To do this, lets jump to a new tab in your browser and copy and paste the following url into the address bar.

https://graph.facebook.com/oauth/authorize?client_id=YOUR_APP_ID&redirect_uri=THE_URL_OF_YOUR_WEBSITE&scope=read_stream

after you hit enter in your browser to goto the url you’ve just specified, this will ask you to login to facebook (if needed) and after you login, you'll be asked to login using your facebook app.

After you allow your app to be used with your facebook account you will be redirected to the redirect_uri you specified before, with a code sent to that page in get parameter. This url will look something like the one below:

http://www.example.com/?code=e3b7fb0d311786a055a51618-10...

We will need this code to request an access_token, which will be used each time we gonna query the Graph API.

Install fbgraph module

npm install fbgraph

Get User info from id

var facebook_client = require('fbgraph');
var facebook_config = {
    client_id: 'YOUR_APP_ID',
    client_secret: 'YOUR_APP_SECRET',
    redirect_uri: 'http://www.example.com/auth/facebook'
};

facebook_client.authorize({
    "client_id": facebook_config.client_id,
    "redirect_uri": facebook_config.redirect_uri,
    "client_secret": facebook_config.client_secret,
    "code": THE_CODE_RETRIEVED_FROM_PREVIOUS_STEP
}, function (err, facebookRes) {
    if(err) {
        console.log(err);
    }
    else{
        if(facebookRes.access_token) {
            facebook_client.setAccessToken(facebookRes.access_token);
            facebook_client.get(id, function(err, res){
            	if(err) console.log(err);
                else console.log(res);
            });
        }
    }
});

We are here just logging the output contained in res variable. You may do whatever you want with the data contained in this variable for your application.