Skip to content

Commit

Permalink
1.4.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
DracoBlue committed Oct 6, 2011
2 parents 7f79e6d + 93a6005 commit f1181c4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 61 deletions.
30 changes: 24 additions & 6 deletions README.md
@@ -1,11 +1,11 @@
node-facebook-client README node-facebook-client README
=========================== ===========================


Version: 1.3.0 Version: 1.4.0


Official Site: <http://dracoblue.net/> Official Site: <http://dracoblue.net/>


node-facebook-client is copyright 2010 by DracoBlue <http://dracoblue.net> node-facebook-client is copyright 2010-2011 by DracoBlue <http://dracoblue.net>


What is node-facebook-client? What is node-facebook-client?
----------------------------- -----------------------------
Expand Down Expand Up @@ -36,20 +36,25 @@ user. requst.headers are the headers from the server request.
})(function(result) { })(function(result) {
console.log('Username is:' + result.name); console.log('Username is:' + result.name);
}); });
facebook_session.graphCall("/me/feed", {message:"I love node.js!"}, 'POST')(function(result) {
console.log('The new feed post id is: ' + result.id);
});
}); });


A full example may be executed with: `node run_example.js`. Please configure `yourappid`+`yourappsecret` in that file first. A full example may be executed with: `node run_example.js`. Please configure `yourappid`+`yourappsecret` in that file first.


## Graph API ## Graph API


### FacebookClient#graphCall(path, params) ### FacebookClient#graphCall(path, params[, method])


Doing a call against the graph server. Doing a call against the graph server.


client.graphCall(path, params)(function(result) { client.graphCall(path, params, method)(function(result) {
// //
}); });


The parameter `method` can be omitted and is 'GET' in this case.

## Rest API ## Rest API


### FacebookSession#restCall(method, params, access_token) ### FacebookSession#restCall(method, params, access_token)
Expand All @@ -74,6 +79,16 @@ Use the request headers to retrieve the session.
// session is either undefined or a valid FacebookSession // session is either undefined or a valid FacebookSession
}); });


### FacebookSession#isValid()

Calls `/me` on the graph api, to check whether the session is still valid or the
user has already logged out.

session.isValid()(function(is_valid) {
// is either true or false
});

Remember to do that only when necessary and not on every request.


### FacebookClient#getSessionByAccessToken(access_token) ### FacebookClient#getSessionByAccessToken(access_token)


Expand Down Expand Up @@ -131,9 +146,12 @@ Calculates the signature for a given set of parameters and the api_secret.
Changelog Changelog
--------- ---------


- 1.3.0 (2011/10/06) - 1.4.0 (2011/10/06)
- fixed handling of missing expire time
- added multiquery-support. #12 - added multiquery-support. #12
- 1.3.0 (2011/04/26)
- added FacebookSession#isValid
- fixed expires validation fixes #5
- added method argument to session.graphCall to permit POSTing in addition to GETting
- 1.2.0 (2011/03/09) - 1.2.0 (2011/03/09)
- added support for node 0.4 - added support for node 0.4
- 1.1.0 (2010/12/29) - 1.1.0 (2010/12/29)
Expand Down
73 changes: 33 additions & 40 deletions lib/facebook-client/FacebookClient.js
Expand Up @@ -15,15 +15,15 @@ var querystring = require("querystring");
var FacebookSession = require("./FacebookSession").FacebookSession; var FacebookSession = require("./FacebookSession").FacebookSession;
var FacebookToolkit = require("./FacebookToolkit"); var FacebookToolkit = require("./FacebookToolkit");


function doRawJsonRequest(host, port, path, secure) { function doRequest(host, port, path, secure, method, data, parser) {
return function(cb) { return function(cb) {
var protocol = http; var protocol = http;
if(secure) protocol = https; if(secure) protocol = https;
var options = { var options = {
host: host, host: host,
port: port, port: port,
path: path, path: path,
method: 'GET' method: method || 'GET'
}; };
var request = protocol.request(options, function(response){ var request = protocol.request(options, function(response){
response.setEncoding("utf8"); response.setEncoding("utf8");
Expand All @@ -35,43 +35,22 @@ function doRawJsonRequest(host, port, path, secure) {
}); });


response.on("end", function () { response.on("end", function () {
cb(JSON.parse(body.join(""))); cb(parser(body.join("")));
}); });
}); });

if(data != null) {
request.write(querystring.stringify(data))
}
request.end(); request.end();
}; };
}; };
function doRawJsonRequest(host, port, path, secure, method, data) {
return doRequest(host, port, path, secure, method, data, JSON.parse);
};



function doRawQueryRequest(host, port, path, secure, method, data) {
function doRawQueryRequest(host, port, path, secure) { return doRequest(host, port, path, secure, method, data, querystring.parse);
return function(cb) { };
var protocol = http;
if(secure) protocol = https;
var options = {
host: host,
port: port,
path: path,
method: 'GET'
};

var request = protocol.request(options, function(response){
response.setBodyEncoding("utf8");

var body = [];

response.on("data", function (chunk) {
body.push(chunk);
});

response.on("end", function () {
cb(querystring.parse(body.join("")));
});
});

request.end();
};
}


var FacebookClient = function(api_key, api_secret, options) { var FacebookClient = function(api_key, api_secret, options) {
var self = this; var self = this;
Expand Down Expand Up @@ -117,17 +96,30 @@ var FacebookClient = function(api_key, api_secret, options) {
return doRestCall(method, params, access_token); return doRestCall(method, params, access_token);
}; };


this.graphCall = function(path, params) { this.graphCall = function(path, params, method) {
/*
* Default to take HTTP because it's faster.
*/
var host = self.options.facebook_graph_server_host;
var port = self.options.facebook_graph_server_port;
var secure = false;
var data = null;

if (params.access_token) { if (params.access_token) {
/* /*
* We have to do a secure request, because the access_token is given. This is HTTPS. * We have to do a secure request, because the access_token is given. This is HTTPS.
*/ */
return doRawJsonRequest(self.options.facebook_graph_secure_server_host, self.options.facebook_graph_secure_server_port, path + '?' + querystring.stringify(params), true); host = self.options.facebook_graph_secure_server_host;
port = self.options.facebook_graph_secure_server_port;
secure = true;
} }
/*
* No access token given, let's take HTTP because it's faster. if (method == 'POST') {
*/ data = params;
return doRawJsonRequest(self.options.facebook_graph_server_host, self.options.facebook_graph_server_port, path + '?' + querystring.stringify(params), false); } else {
path = path + '?' + querystring.stringify(params);
}
return doRawJsonRequest(host, port, path, secure, method, data);
}; };


this.getAccessToken = function(params) { this.getAccessToken = function(params) {
Expand Down Expand Up @@ -205,8 +197,9 @@ FacebookClient.prototype.getSessionByRequestHeaders = function(request_headers)
* The token is expired. * The token is expired.
*/ */
cb(); cb();
return ;
} }

self.getSessionByAccessToken(facebook_cookie['access_token'])(cb); self.getSessionByAccessToken(facebook_cookie['access_token'])(cb);
}; };
}; };
Expand Down
29 changes: 26 additions & 3 deletions lib/facebook-client/FacebookSession.js
Expand Up @@ -16,7 +16,8 @@ var FacebookSession = function(facebook_client, access_token) {


if (access_token) if (access_token)
{ {
self.graphCall = function(path, params) { self.graphCall = function(path, params, method) {
method = method || 'GET';
var authed_params = { var authed_params = {
"access_token": access_token "access_token": access_token
}; };
Expand All @@ -25,7 +26,7 @@ var FacebookSession = function(facebook_client, access_token) {
authed_params[key] = params[key]; authed_params[key] = params[key];
} }


return self.facebook_client.graphCall(path, authed_params); return self.facebook_client.graphCall(path, authed_params, method);
}; };


this.restCall = function(method, params) { this.restCall = function(method, params) {
Expand Down Expand Up @@ -55,6 +56,28 @@ FacebookSession.prototype.getId = function() {
} }
} }


FacebookSession.prototype.isValid = function() {
var self = this;

return function(cb) {
if (self.has_access_token) {
self.graphCall("/me")(function(user_data){
if (user_data.error)
{
cb(false);
}
else
{
cb(true);
}
});
} else {
cb(false);
}
}
}


FacebookSession.prototype.getMeta = function() { FacebookSession.prototype.getMeta = function() {
var self = this; var self = this;


Expand Down Expand Up @@ -110,4 +133,4 @@ FacebookSession.prototype.injectAccessToken = function(access_token) {
}; };
}; };


exports.FacebookSession = FacebookSession; exports.FacebookSession = FacebookSession;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ {
"name" : "facebook-client", "name" : "facebook-client",
"version": "1.2.0", "version": "1.4.0",


"engines": { "node": ">= 0.4.0" }, "engines": { "node": ">= 0.4.0" },


Expand Down
31 changes: 20 additions & 11 deletions run_example.js
Expand Up @@ -43,18 +43,27 @@ http.createServer(function (request, response) {
/* /*
* Graph-API * Graph-API
*/ */
facebook_session.graphCall("/me", { response.writeHead(200, {'Content-Type': 'text/plain'});
})(function(result) { facebook_session.isValid()(function(is_valid) {
response.writeHead(200, {'Content-Type': 'text/plain'}); if (!is_valid)
response.write('By using Graph API:' + "\n"); {
response.write(' Name:' + result.name + "\n"); response.write('Session expired or user logged out.' + "\n");
response.write(' Id:' + result.id + "\n");
response.write(' Link:' + result.link + "\n");
facebook_session.restCall("fql.multiquery", {"queries": {"query1":"SELECT uid FROM user WHERE uid=" + result.id, "query2":"SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)"}}, {})(function() {
console.log('multiquery', JSON.stringify(arguments[0]));
response.end(); response.end();
}); return ;
}); }
facebook_session.graphCall("/me", {
})(function(result) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('By using Graph API:' + "\n");
response.write(' Name:' + result.name + "\n");
response.write(' Id:' + result.id + "\n");
response.write(' Link:' + result.link + "\n");
facebook_session.restCall("fql.multiquery", {"queries": {"query1":"SELECT uid FROM user WHERE uid=" + result.id, "query2":"SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)"}}, {})(function() {
console.log('multiquery', JSON.stringify(arguments[0]));
response.end();
});
});
});
}); });


}).listen(8000); }).listen(8000);

0 comments on commit f1181c4

Please sign in to comment.