Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 2f5360167b56945e6938d27905b479bd08da3349 @DracoBlue committed Oct 5, 2010
21 LICENSE
@@ -0,0 +1,21 @@
+node-facebook-client is licensed under the terms of MIT License.
+
+Copyright (c) 2010 by DracoBlue (JanS@DracoBlue.de)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
147 README.md
@@ -0,0 +1,147 @@
+node-facebook-client README
+===========================
+
+Version: 1.0.0
+
+Official Site: <http://dracoblue.net/>
+
+node-facebook-client is copyright 2010 by DracoBlue <http://dracoblue.net>
+
+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.
+
+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.
+
+ // 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
+ "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 + '!');
+ }
+ });
+ });
+
+
+## Graph API
+
+### FacebookClient#graphCall(path, params)
+
+Doing a call against the graph server.
+
+ client.graphCall()(function(access_token, expires) {
+ //
+ });
+
+## Rest API
+
+### FacebookClient#restCall(method, params)
+
+Doing a signed call against the rest api server.
+
+### FacebookSession#restCall(method, params)
+
+Doing a signed call against the rest api server, by using the session of the
+user.
+
+ session.restCall("users.getInfo", {
+ fields: "name",
+ uids: session.uid
+ })(function(response_users) {
+ // work with it
+ });
+
+### FacebookClient#restCallUnsigned(method, params)
+
+Doing an unsigned call against the rest api server.
+
+## General API
+
+### FacebookClient#getSessionByKey(session_key)
+
+Creating a new FacebookSession instance with the given session_key.
+
+ client.getSessionByKey(session.session_key)(function(session) {
+ // work with the key,
+ session.restCall("users.getInfo", {
+ fields: "name",
+ uids: session.uid
+ })(function(response_users) {
+ // yay, we got it's name: response_users[0].name!
+ });
+ });
+
+### FacebookClient#getSessionByAccessToken(access_token)
+
+Creating a new FacebookSession instance with a given access_token.
+
+### FacebookSession#getId()
+
+Retrieving the id of the session.
+
+ session.getId()(function(id) {
+ // is either a string or undefined, in case the session has no id
+ });
+
+### FacebookSession#getMeta()
+
+Tries to retrieve all data from the graph call /me for the user. This is
+_only_ available in case of a session, which got initialized by an access_token.
+
+ session.getMeta()(function(user_data) {
+ // work with it
+ });
+
+## Internal API
+
+### FacebookClient#getAccessToken(access_params)
+
+Retrieving an AccessToken with the given parameters.
+
+ client.getAccessToken(access_params)(function(access_token, expires) {
+ //
+ });
+
+### FacebookSession#retrieveAccessToken(code, redirect_uri)
+
+Retrieve an access token by providing a code and a redirect_uri. Usually from
+successful oauth redirect.
+
+### FacebookSession#injectAccessToken(access_token)
+
+Used to inject an access_token into an existing FacebookSession.
+
+### FacebookSession#getAccessToken(access_params)
+
+Retrieving an AccessToken with the given parameters and injecting it into the
+FacebookSession.
+
+### FacebookToolkit.generateSignature(params, api_secret)
+
+Calculates the signature for a given set of parameters and the api_secret.
+
+License
+--------
+
+node-facebook-client is licensed under the terms of MIT. See LICENSE for more information.
@@ -0,0 +1,169 @@
+/*
+ * This file is part of node-facebook-client
+ *
+ * Copyright (c) 2010 DracoBlue, http://dracoblue.net/
+ *
+ * Licensed under the terms of MIT License. For the full copyright and license
+ * information, please see the LICENSE file in the root folder.
+ */
+
+var sys = require("sys");
+var http = require("http");
+var querystring = require("querystring");
+
+var FacebookSession = require("./FacebookSession").FacebookSession;
+var FacebookToolkit = require("./FacebookToolkit");
+
+function doRawJsonRequest(host, port, path) {
+ return function(cb) {
+ var connection = http.createClient(port, host);
+ var request = connection.request('GET', path, {'host': host});
+
+ request.addListener('response', function(response){
+ response.setEncoding("utf8");
+
+ var body = [];
+
+ response.addListener("data", function (chunk) {
+ body.push(chunk);
+ });
+
+ response.addListener("end", function () {
+ cb(JSON.parse(body.join("")));
+ });
+ });
+
+ request.end();
+ };
+};
+
+
+function doRawQueryRequest(host, port, path) {
+ return function(cb) {
+ var connection = http.createClient(port, host);
+ var request = connection.request('GET', path, {'host': host});
+
+ request.addListener('response', function(response){
+ response.setBodyEncoding("utf8");
+
+ var body = [];
+
+ response.addListener("data", function (chunk) {
+ body.push(chunk);
+ });
+
+ response.addListener("end", function () {
+ cb(querystring.parse(body.join("")));
+ });
+ });
+
+ request.end();
+ };
+}
+
+var FacebookClient = function(api_key, api_secret, options) {
+ var self = this;
+
+ this.options = 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_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];
+ }
+
+ var request_path_array = [self.options.facebook_server_path, '?'];
+
+ var is_first = true;
+ for (var key in request_params) {
+ if (is_first) {
+ is_first = false;
+ } else {
+ request_path_array.push('&');
+ }
+ 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)));
+ }
+
+ return doRawJsonRequest(self.options.facebook_server_host, self.options.facebook_server_port, request_path_array.join(''));
+ };
+
+ this.restCallUnsigned = function(method, params) {
+ return doRestCall(method, params, false);
+ };
+
+ this.restCall = function(method, params) {
+ return doRestCall(method, params, true);
+ };
+
+ this.graphCall = function(path, params) {
+ return doRawJsonRequest(self.options.facebook_graph_server_host, self.options.facebook_graph_server_port, path + '?' + querystring.stringify(params));
+ };
+
+ this.getAccessToken = function(params) {
+ var access_params = {
+ client_id: api_key,
+ client_secret: api_secret
+ };
+
+ for (var key in params) {
+ access_params[key] = params[key];
+ }
+
+ return function(cb) {
+ doRawQueryRequest(self.options.facebook_graph_server_host, self.options.facebook_graph_server_port, "/oauth/access_token" + '?' + querystring.stringify(access_params))(function(response) {
+ cb(response.access_token, response.expires);
+ });
+ };
+ };
+
+};
+
+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);
+ });
+ };
+};
+
+// 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;
Oops, something went wrong.

0 comments on commit 2f53601

Please sign in to comment.