Browse files

1.4.0 release

  • Loading branch information...
2 parents 7f79e6d + 93a6005 commit f1181c43faf09eb5afd7d919ad8060b1d548d534 @DracoBlue committed Oct 6, 2011
Showing with 104 additions and 61 deletions.
  1. +24 −6 README.md
  2. +33 −40 lib/facebook-client/FacebookClient.js
  3. +26 −3 lib/facebook-client/FacebookSession.js
  4. +1 −1 package.json
  5. +20 −11 run_example.js
View
30 README.md
@@ -1,11 +1,11 @@
node-facebook-client README
===========================
-Version: 1.3.0
+Version: 1.4.0
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?
-----------------------------
@@ -36,20 +36,25 @@ user. requst.headers are the headers from the server request.
})(function(result) {
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.
## Graph API
-### FacebookClient#graphCall(path, params)
+### FacebookClient#graphCall(path, params[, method])
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
### FacebookSession#restCall(method, params, access_token)
@@ -74,6 +79,16 @@ Use the request headers to retrieve the session.
// 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)
@@ -131,9 +146,12 @@ Calculates the signature for a given set of parameters and the api_secret.
Changelog
---------
-- 1.3.0 (2011/10/06)
- - fixed handling of missing expire time
+- 1.4.0 (2011/10/06)
- 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)
- added support for node 0.4
- 1.1.0 (2010/12/29)
View
73 lib/facebook-client/FacebookClient.js
@@ -15,15 +15,15 @@ var querystring = require("querystring");
var FacebookSession = require("./FacebookSession").FacebookSession;
var FacebookToolkit = require("./FacebookToolkit");
-function doRawJsonRequest(host, port, path, secure) {
+function doRequest(host, port, path, secure, method, data, parser) {
return function(cb) {
var protocol = http;
if(secure) protocol = https;
var options = {
host: host,
port: port,
path: path,
- method: 'GET'
+ method: method || 'GET'
};
var request = protocol.request(options, function(response){
response.setEncoding("utf8");
@@ -35,43 +35,22 @@ function doRawJsonRequest(host, port, path, secure) {
});
response.on("end", function () {
- cb(JSON.parse(body.join("")));
+ cb(parser(body.join("")));
});
});
-
+ if(data != null) {
+ request.write(querystring.stringify(data))
+ }
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) {
- 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();
- };
-}
+function doRawQueryRequest(host, port, path, secure, method, data) {
+ return doRequest(host, port, path, secure, method, data, querystring.parse);
+};
var FacebookClient = function(api_key, api_secret, options) {
var self = this;
@@ -117,17 +96,30 @@ var FacebookClient = function(api_key, api_secret, options) {
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) {
/*
* 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.
- */
- return doRawJsonRequest(self.options.facebook_graph_server_host, self.options.facebook_graph_server_port, path + '?' + querystring.stringify(params), false);
+
+ if (method == 'POST') {
+ data = params;
+ } else {
+ path = path + '?' + querystring.stringify(params);
+ }
+ return doRawJsonRequest(host, port, path, secure, method, data);
};
this.getAccessToken = function(params) {
@@ -205,8 +197,9 @@ FacebookClient.prototype.getSessionByRequestHeaders = function(request_headers)
* The token is expired.
*/
cb();
+ return ;
}
-
+
self.getSessionByAccessToken(facebook_cookie['access_token'])(cb);
};
};
View
29 lib/facebook-client/FacebookSession.js
@@ -16,7 +16,8 @@ var FacebookSession = function(facebook_client, access_token) {
if (access_token)
{
- self.graphCall = function(path, params) {
+ self.graphCall = function(path, params, method) {
+ method = method || 'GET';
var authed_params = {
"access_token": access_token
};
@@ -25,7 +26,7 @@ var FacebookSession = function(facebook_client, access_token) {
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) {
@@ -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() {
var self = this;
@@ -110,4 +133,4 @@ FacebookSession.prototype.injectAccessToken = function(access_token) {
};
};
-exports.FacebookSession = FacebookSession;
+exports.FacebookSession = FacebookSession;
View
2 package.json
@@ -1,6 +1,6 @@
{
"name" : "facebook-client",
- "version": "1.2.0",
+ "version": "1.4.0",
"engines": { "node": ">= 0.4.0" },
View
31 run_example.js
@@ -43,18 +43,27 @@ http.createServer(function (request, response) {
/*
* Graph-API
*/
- 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.writeHead(200, {'Content-Type': 'text/plain'});
+ facebook_session.isValid()(function(is_valid) {
+ if (!is_valid)
+ {
+ response.write('Session expired or user logged out.' + "\n");
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);

0 comments on commit f1181c4

Please sign in to comment.