Created
November 30, 2011 01:53
-
-
Save joshsmith/1407607 to your computer and use it in GitHub Desktop.
Facebook Client for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facebook Client for Node.js | |
* @author Josh Smith | |
*/ | |
var qs = require('querystring'), | |
request = require('request'), | |
conf = require('../config/conf'); | |
var Facebook = exports.Facebook = function(userId, accessToken) { | |
var url = 'https://graph.facebook.com/' + userId + '/'; | |
this.accessToken = accessToken; | |
this.post = function (path,data,callback) { | |
url += path; | |
request.post({uri:url, body:data}, function(e, r, data) { | |
if(e) { | |
if (typeof e == 'string') | |
e = JSON.parse(e); | |
else if (typeof e.data == 'string') | |
e.data = JSON.parse(e.data); | |
} | |
callback(e, data, r); | |
}); | |
} | |
this.get = function (path,callback) { | |
url += path; | |
console.log(url); | |
request.get({url:url}, function(e, r, data) { | |
if(e) { | |
if (typeof e == 'string') | |
e = JSON.parse(e); | |
else if (typeof e.data == 'string') | |
e.data = JSON.parse(e.data); | |
} else { | |
var data = JSON.parse(data); | |
callback(e, data, r); | |
} | |
}); | |
} | |
} | |
Facebook.prototype.getLikes = function (callback) { | |
var args = { | |
'access_token' : this.accessToken | |
} | |
var path = 'likes?' + qs.stringify(args); | |
this.get(path, callback); | |
} | |
Facebook.prototype.getFriends = function (callback) { | |
var args = { | |
'access_token' : this.accessToken | |
} | |
var path = 'friends?' + qs.stringify(args); | |
this.get(path, callback); | |
} | |
Facebook.prototype.postToWall = function (message, params, callback) { | |
if (typeof params == 'function') { | |
callback = params; | |
params = []; | |
} | |
var args = { | |
'access_token' : this.accessToken | |
} | |
var json = { | |
'message' : message | |
} | |
if(params.hasOwnProperty("attachment")) { | |
json.attachment = params.attachment; | |
} | |
if(params.hasOwnProperty("actions")) { | |
var actions = params.actions; | |
} | |
if(params.hasOwnProperty("target_id")) { | |
json.target_id = params.target_id; | |
} | |
if(params.hasOwnProperty("uid")) { | |
json.uid = params.uid; | |
} | |
if(params.hasOwnProperty("privacy")) { | |
json.privacy = params.privacy; | |
} | |
var data = qs.stringify(json); | |
var action_links = JSON.stringify(actions); | |
data += '&actions=' + action_links; | |
var path = 'feed?' + qs.stringify(args); | |
this.post(path, data, callback); | |
} | |
var fb = new Facebook('APP_ID', 'ACCESS_TOKEN'); | |
var message = "This is a test."; | |
var params = { | |
actions: [ {'name' : 'Test Link', 'link' : 'http://google.com'}] | |
} | |
fb.postToWall(message, params, function(e, data, r) { | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment