public
Last active

Simple Facebook Graph API Node.js Client

  • Download Gist
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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;
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
"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"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.