Skip to content

Instantly share code, notes, and snippets.

@shanestillwell
Forked from jankuca/index.js
Created August 20, 2012 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanestillwell/3408301 to your computer and use it in GitHub Desktop.
Save shanestillwell/3408301 to your computer and use it in GitHub Desktop.
Simple Facebook Graph API Node.js Client
var HTTPS = require('https');
var QueryString = require('querystring');
/**
* Facebook API Wrapper
* @constructor
* @param {Object} info Info about a Facebook application
*/
var Client = function (info) {
if (!info.KEY || !info.SECRET || !info.ID) {
throw new Error('Missing some info');
}
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 () {
try {
var result = JSON.parse(data);
if (result['error'] || result['error_code']) {
callback(result, null);
} else {
callback(null, result);
}
} catch (err) {
callback(res.statusCode !== 200, data || null);
}
});
});
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;
params.format = params.format || 'json';
params.locale = params.locale || 'en_US';
if (!params.client_id) {
var authenticating = /^\/oauth\//.test(uri) || /^\/dialog\//.test(uri);
if (authenticating) {
params.client_id = this.info.KEY;
}
}
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.0.0",
"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