Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* 1.1.0

* added example
* using legacy rest api server now
* using only access token now (session_key has been removed)
  • Loading branch information...
commit 6b8502eacb31874565f277d92a6eaa85dff67746 1 parent 5ef2d43
@DracoBlue DracoBlue authored
View
39 README.md
@@ -1,7 +1,7 @@
node-facebook-client README
===========================
-Version: 1.0-dev
+Version: 1.1.0
Official Site: <http://dracoblue.net/>
@@ -12,37 +12,33 @@ What is node-facebook-client?
The node-facebook-client library is a set of nodejs classes to communicate
with the rest and graph api provided by facebook.
+It works great if you embed the facebook connect button on your page and
+want to use the rest + graph api by facebook. Oauth-support may work but
+is not tested that well.
+
The library is not officially created nor maintained by facebook. It is
created by dracoblue and licensed under the terms of MIT License.
## Example
-This small example uses the FacebookClient class to retrieve the name of a user.
+This small example uses the FacebookClient class to retrieve the name of a
+user. requst.headers are the headers from the server request.
- // Input: session_key : the session_key, taken from fb cookies)
- // user_id : facebook id of a user for users.getInfo
-
var FacebookClient = require("facebook-client").FacebookClient;
-
+
var facebook_client = new FacebookClient(
- "yourappid", // configure like your fb app page states
+ "yourappid", // configure like your fb app page states
"yourappsecret" // configure like your fb app page states
);
- facebook_client.getSessionByKey(session_key)(function(facebook_session) {
- facebook_session.restCall("users.getInfo", {
- fields: "name",
- uids: user_id
- })(function(response_users) {
- if (response_users.error_code) {
- // User does not exist :(
- } else {
- // We got the data!
- console.log('Hi ' + response_users[0].name + '!');
- }
- });
- });
+ facebook_client.getSessionByRequestHeaders(request.headers)(function(facebook_session) {
+ facebook_session.graphCall("/me", {
+ })(function(result) {
+ console.log('Username is:' + result.name);
+ });
+ });
+A full example may be executed with: `node run_example.js`. Please configure `yourappid`+`yourappsecret` in that file first.
## Graph API
@@ -144,6 +140,9 @@ Calculates the signature for a given set of parameters and the api_secret.
Changelog
---------
+- 1.1.0 (2010/12/29)
+ - removed session_key support
+ - added example
- 1.0.1 (2010/12/29)
- added secure url for access_token
- 1.0.0 (2010/10/05)
View
101 lib/facebook-client/FacebookClient.js
@@ -68,29 +68,22 @@ var FacebookClient = function(api_key, api_secret, options) {
this.options.facebook_graph_server_host = this.options.facebook_graph_server_host || 'graph.facebook.com';
this.options.facebook_graph_server_port = this.options.facebook_graph_server_port || '80';
- this.options.facebook_graph_server_port = this.options.facebook_graph_secure_server_port || '443';
- this.options.facebook_graph_server_host = this.options.facebook_graph_secure_server_host || 'graph.facebook.com';
+ this.options.facebook_graph_secure_server_port = this.options.facebook_graph_secure_server_port || '443';
+ this.options.facebook_graph_secure_server_host = this.options.facebook_graph_secure_server_host || 'graph.facebook.com';
this.options.facebook_server_host = this.options.facebook_server_host || 'api.facebook.com';
- this.options.facebook_server_port = this.options.facebook_server_port || '80';
- this.options.facebook_server_path = this.options.facebook_server_path || '/restserver.php';
-
- var doRestCall = function(method, params, calculate_signature) {
- var request_params = {
- "method": method,
- "v": "1.0",
- "format": "json",
- "api_key": api_key
- };
-
- for (var key in params) {
- request_params[key] = params[key];
- }
+ this.options.facebook_server_port = this.options.facebook_server_port || '443';
+ this.options.facebook_server_path = this.options.facebook_server_path || '/method/';
- var request_path_array = [self.options.facebook_server_path, '?'];
+ var doRestCall = function(method, params, access_token) {
+ var request_path_array = [self.options.facebook_server_path, method, '?'];
+
+ params = params || {};
+ params['access_token'] = access_token;
+ params['format'] = "json";
var is_first = true;
- for (var key in request_params) {
+ for (var key in params) {
if (is_first) {
is_first = false;
} else {
@@ -98,23 +91,14 @@ var FacebookClient = function(api_key, api_secret, options) {
}
request_path_array.push(encodeURIComponent(key));
request_path_array.push("=");
- request_path_array.push(encodeURIComponent(request_params[key]));
- }
-
- if (calculate_signature) {
- request_path_array.push("&sig=");
- request_path_array.push(encodeURIComponent(FacebookToolkit.generateSignature(request_params,api_secret)));
+ request_path_array.push(encodeURIComponent(params[key]));
}
-
- return doRawJsonRequest(self.options.facebook_server_host, self.options.facebook_server_port, request_path_array.join(''));
+
+ return doRawJsonRequest(self.options.facebook_server_host, self.options.facebook_server_port, request_path_array.join(''), true);
};
- this.restCallUnsigned = function(method, params) {
- return doRestCall(method, params, false);
- };
-
- this.restCall = function(method, params) {
- return doRestCall(method, params, true);
+ this.restCall = function(method, params, access_token) {
+ return doRestCall(method, params, access_token);
};
this.graphCall = function(path, params) {
@@ -149,20 +133,11 @@ var FacebookClient = function(api_key, api_secret, options) {
};
-FacebookClient.prototype.getSessionByKey = function(session_key) {
- var self = this;
- return function(cb) {
- cb(new FacebookSession(self, session_key));
- };
-};
-
FacebookClient.prototype.getSessionByAccessToken = function(access_token) {
var self = this;
return function(cb) {
- var session = new FacebookSession(self);
- session.injectAccessToken(access_token)(function() {
- cb(session);
- });
+ var session = new FacebookSession(self, access_token);
+ cb(session);
};
};
@@ -198,49 +173,15 @@ FacebookClient.prototype.getSessionByRequestHeaders = function(request_headers)
return ;
}
- var session = null;
-
- if (!facebook_cookie['session_key'] && !facebook_cookie['access_token']) {
+ if (!facebook_cookie['access_token']) {
/*
- * We have neither session_key nor access_token. This won't work.
+ * We don't have an access_token, this won't work.
*/
cb();
}
-
- /*
- * We can create it from session key, if it is available.
- */
- if (facebook_cookie['session_key']) {
- session = new FacebookSession(self, facebook_cookie['session_key']);
- } else {
- session = new FacebookSession(self);
- }
- /*
- * Let's see if we have an access token
- */
- if (facebook_cookie['access_token']) {
- session.injectAccessToken(facebook_cookie['access_token'])(function() {
- cb(session);
- });
- return ;
- }
-
- cb(session);
+ self.getSessionByAccessToken(facebook_cookie['access_token'])(cb);
};
};
-
-
-// TODO: Let's see if we still need this.
-//FacebookClient.prototype.getSessionByCodeAndRedirectUri = function(code, redirect_uri) {
-// var self = this;
-// return function(cb) {
-// var session = new FacebookSession(self);
-// session.retrieveAccessToken(code, redirect_uri)(function() {
-// cb(session);
-// });
-// };
-//};
-
exports.FacebookClient = FacebookClient;
View
67 lib/facebook-client/FacebookSession.js
@@ -7,31 +7,33 @@
* information, please see the LICENSE file in the root folder.
*/
-var FacebookSession = function(facebook_client, session_key) {
+var FacebookSession = function(facebook_client, access_token) {
var self = this;
this.facebook_client = facebook_client;
- if (session_key) {
- this.has_session_key = true;
- } else {
- this.has_session_key = false;
- }
-
this.has_access_token = false;
- this.restCall = function(method, params) {
- var authed_params = {
- "session_key": session_key
+ if (access_token)
+ {
+ self.graphCall = function(path, params) {
+ var authed_params = {
+ "access_token": access_token
+ };
+
+ for (var key in params) {
+ authed_params[key] = params[key];
+ }
+
+ return self.facebook_client.graphCall(path, authed_params);
+ };
+
+ this.restCall = function(method, params) {
+ return facebook_client.restCall(method, params, access_token);
};
- for (var key in params) {
- authed_params[key] = params[key];
- }
-
- return facebook_client.restCall(method, authed_params);
- };
-
+ self.has_access_token = true;
+ }
this.getAccessToken = function(options) {
return facebook_client.getAccessToken(options);
@@ -47,11 +49,6 @@ FacebookSession.prototype.getId = function() {
self.graphCall("/me")(function(user_data){
cb(user_data.id);
});
- } else if (self.has_session_key) {
- self.restCall("users.getLoggedInUser", {
- })(function(ret_val) {
- cb(ret_val);
- });
} else {
cb();
}
@@ -66,8 +63,6 @@ FacebookSession.prototype.getMeta = function() {
self.graphCall("/me")(function(user_data){
cb(user_data);
});
- } else if (self.has_session_key) {
- throw new Error('getMeta is not yet implemented, if you don\'t have an access_token.');
} else {
cb();
}
@@ -82,21 +77,9 @@ FacebookSession.prototype.retrieveAccessToken = function(code, redirect_uri) {
redirect_uri: redirect_uri,
code: code
})(function(access_token, expire_time) {
- self.graphCall = function(path, params) {
- var authed_params = {
- "access_token": access_token
- };
-
- for (var key in params) {
- authed_params[key] = params[key];
- }
-
- return self.facebook_client.graphCall(path, authed_params);
- };
-
- self.has_access_token = true;
-
- cb();
+ self.injectAccessToken(access_token)(function() {
+ cb();
+ });
});
}
};
@@ -116,11 +99,15 @@ FacebookSession.prototype.injectAccessToken = function(access_token) {
return self.facebook_client.graphCall(path, authed_params);
};
+
+ self.restCall = function(method, params) {
+ return facebook_client.restCall(method, params, access_token);
+ };
self.has_access_token = true;
cb();
- }
+ };
};
exports.FacebookSession = FacebookSession;
View
2  package.json
@@ -1,6 +1,6 @@
{
"name" : "facebook-client",
- "version": "1.0.1",
+ "version": "1.1.0",
"engines": { "node": ">= 0.2.0" },
View
56 run_example.js
@@ -0,0 +1,56 @@
+var http = require('http');
+var FacebookClient = require("facebook-client").FacebookClient;
+
+http.createServer(function (request, response) {
+ var app_id = "yourappid"; // configure like your fb app page states
+
+ var facebook_client = new FacebookClient(
+ app_id,
+ "yourappsecret" // configure like your fb app page states
+ );
+
+ facebook_client.getSessionByRequestHeaders(request.headers)(function(facebook_session) {
+ if (!facebook_session)
+ {
+ response.writeHead(200, {'Content-Type': 'text/html'});
+ response.write([
+ '<html>',
+ '<head><title>node-facebook-client example</title></head><body>',
+ '<p>Login please</p> <fb:login-button autologoutlink="true"></fb:login-button>',
+ '<div id="fb-root"></div>',
+ '<script type="text/javascript">',
+ 'window.fbAsyncInit = function() {',
+
+ ' FB.init({appId: "' + app_id +'", logging:false, status: true, cookie: true, xfbml: true});',
+ ' FB.Event.subscribe(\'auth.sessionChange\', function(response) {',
+ ' document.location = document.location.href;',
+ ' });',
+ '};',
+ '(function() {',
+ ' var e = document.createElement(\'script\'); e.async = true;',
+ ' e.src = document.location.protocol +',
+ ' \'//connect.facebook.net/en_US/all.js\';',
+ ' document.getElementById(\'fb-root\').appendChild(e);',
+ '}());',
+ '</script>',
+ '</body>',
+ '</html>'
+ ].join("\n"));
+ response.end();
+ return ;
+ }
+
+ /*
+ * 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(' Link:' + result.link + "\n");
+ response.end();
+ });
+ });
+
+}).listen(8000);
Please sign in to comment.
Something went wrong with that request. Please try again.