Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple Facebook Graph API Node.js Client
var HTTPS = require('https');
var QueryString = require('querystring');
/**
* Facebook API Wrapper
* @constructor
* @param {Object} info Info about the Facebook application
*/
var Client = function (info) {
if (!info.id) {
throw new Error('Missing Facebook App ID');
}
this.info = info;
this.access_token = null;
};
/**
* Performs a GET request to Facebook API with proper parameters
* @param {string} uri The URI from which to request data
* @param {Object=} params Request parameters
* @param {function(?Error, Object)} callback A callback function
*/
Client.prototype.get = function (uri, params, callback) {
if (arguments.length === 2) {
callback = arguments[1];
params = null;
}
return this.request('GET', uri, params, null, callback);
};
/**
* Performs a POST request to Facebook API with proper parameters
* @param {string} uri The URI from which to request data
* @param {Object} data Request body
* @param {function(?Error, Object)} callback A callback function
*/
Client.prototype.post = function (uri, data, callback) {
return this.request('POST', uri, null, data, callback);
};
/**
* Performs an HTTP request to Facebook API with proper parameters
* @param {string} method The HTTP method to use
* @param {string} uri The URI from which to request data
* @param {Object=} params Request parameters
* @param {Object} data Request body
* @param {function(?Error, Object)} callback A callback function
*/
Client.prototype.request = function (method, uri, params, data, callback) {
params = params || {};
// build a complete pathname
uri = this.getURI(uri, params);
// choose a hostname according to the URI format
var host = /^\/method\//.test(uri) ? 'api.facebook.com' : 'graph.facebook.com';
var req = HTTPS.request({
'method': method,
'host': host,
'port': 443,
'path': uri
}, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var result;
try {
result = JSON.parse(data);
} catch (err) {
return callback(res.statusCode !== 200, data || null);
}
if (result['error'] || result['error_code']) {
callback(result, null);
} else {
callback(null, result);
}
});
});
req.on('error', function (err) {
callback(err);
});
if (data) {
req.write(QueryString.stringify(data));
}
req.end();
};
/**
* Build a complete URI
* @param {string} uri The pathname to use
* @param {!Object} params The parameters to use
* @param {boolean=} abs Whether to return an absolute URI
* @return {string}
*/
Client.prototype.getURI = function (uri, params, abs) {
uri = (uri[0] !== '/') ? '/' + uri : uri;
if (!/^\/dialog\//.test(uri)) {
params.format = params.format || 'json';
}
params.locale = params.locale || 'en_US';
if (!params.client_id) {
if (/\/oauth(\/|$)/.test(uri)) {
params.client_id = this.info.id;
} else if (/^\/dialog\//.test(uri)) {
params.app_id = this.info.id;
}
}
if (!params.client_secret && uri === '/oauth/access_token') {
params.client_secret = this.info.secret;
}
if (!params.access_token && this.access_token) {
params.access_token = this.access_token;
}
var search = QueryString.stringify(params);
var relative = uri + (search ? '?' + search : '');
if (abs) {
var subdomain = 'graph';
if (/^\/method\//.test(uri)) {
subdomain = 'api';
} else if (/^\/dialog\//.test(uri)) {
subdomain = 'www';
}
return 'https://' + subdomain + '.facebook.com' + relative;
}
return relative;
};
/**
* Requests an access token to use when performing operations on a user's behalf
* @param {Object} params Parameters to use
* @param {function(?Error, string)} callback A callback function
*/
Client.prototype.getAccessToken = function (params, callback) {
this.get('/oauth/access_token', params, function (err, data) {
if (!err) {
var result = QueryString.parse(data);
callback(null, result.access_token);
} else {
callback(err, null);
}
});
};
/**
* Requests an application access token to use when managing the application
* @param {function(?Error, string)} callback A callback function
*/
Client.prototype.getAppAccessToken = function (callback) {
var params = {
'client_id': this.info.id,
'grant_type': 'client_credentials'
};
this.get('/oauth/access_token', params, function (err, data) {
if (!err) {
var result = QueryString.parse(data);
callback(null, result.access_token);
} else {
callback(err, null);
}
});
};
module.exports = Client;
{
"name": "node-fb",
"version": "1.1.1",
"description": "A simple Facebook API Wrapper for Node.js applications",
"keywords": [ "facebook", "fb", "api", "graph api", "api wrapper" ],
"author": {
"name": "Jan Kuča",
"email": "jan@jankuca.com",
"url": "http://jankuca.com"
},
"repository": {
"type": "git",
"url": "http://gist.github.com/874070"
},
"engines": {
"node": ">=0.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.