1 | # Stay Classy, Facebook
|
2 |
|
3 | [FBgraph](http://criso.github.com/fbgraph/) is a nodejs module that provides easy access to the facebook graph api
|
4 |
|
5 |
|
6 | ## Oh nooooooesss - MOAR facebook
|
7 |
|
8 | I created this because I wanted to access FB's graph from `node`.
|
9 | The libraries I found, felt clunky to me, and I needed an excuse to create a node module.
|
10 |
|
11 | All calls will return `json`. Facebook sometimes (on friend requests, deleting test users, access token request)
|
12 | decides to just return a `string` or `true` or redirects directly to the image. I say __nay-nay__! Let's make it Disney, and keep things consistent!
|
13 |
|
14 |
|
15 | ## Installation via npm
|
16 | $ npm install fbgraph
|
17 |
|
18 | var graph = require('fbgraph');
|
19 |
|
20 | ## Authentication
|
21 |
|
22 | If you get an accesstoken via some other Oauth module like [everyauth](https://github.com/bnoguchi/everyauth) ,
|
23 | [connect-auth](https://github.com/ciaranj/connect-auth) or [node-oauth](https://github.com/ciaranj/node-oauth) you can just set
|
24 | the access token directly. Most `get` calls, and pretty much all `post` calls will require an `access_token`
|
25 |
|
26 | ```js
|
27 | graph.setAccessToken(access_token);
|
28 | ```
|
29 |
|
30 | This is how you would get authenticated using only the `fbgraph` module.
|
31 | More details below on the __express app__ section
|
32 |
|
33 | ```js
|
34 | // get authorization url
|
35 | var authUrl = graph.getOauthUrl({
|
36 | "client_id": conf.client_id
|
37 | , "redirect_uri": conf.redirect_uri
|
38 | });
|
39 |
|
40 | // shows dialog
|
41 | res.redirect(authUrl);
|
42 |
|
43 | // after user click, auth `code` will be set
|
44 | // we'll send that and get the access token
|
45 | graph.authorize({
|
46 | "client_id": conf.client_id
|
47 | , "redirect_uri": conf.redirect_uri
|
48 | , "client_secret": conf.client_secret
|
49 | , "code": req.query.code
|
50 | }, function (err, facebookRes) {
|
51 | res.redirect('/loggedIn');
|
52 | });
|
53 | ```
|
54 |
|
55 | ## How requests are made
|
56 | All calls are made using the [request](https://github.com/mikeal/request) nodejs module
|
57 | __Why?__ something to do with wheels and re-invention.
|
58 |
|
59 | Request options are directly mapped and can be set like so:
|
60 |
|
61 | ```js
|
62 | var options = {
|
63 | timeout: 3000
|
64 | , pool: { maxSockets: Infinity }
|
65 | , headers: { connection: "keep-alive" }
|
66 | };
|
67 |
|
68 | graph
|
69 | .setOptions(options)
|
70 | .get("zuck", function(err, res) {
|
71 | console.log(res); // { id: '4', name: 'Mark Zuckerberg'... }
|
72 | });
|
73 | ```
|
74 |
|
75 | Possible options can be found on the [request github page](https://github.com/mikeal/request)
|
76 |
|
77 | `followRedirect` cannot be overriden and has a default value of `false`
|
78 | `encoding` will have `utf-8` as default if nothing is set
|
79 |
|
80 | ## Read data from the Graph Api
|
81 |
|
82 | ```js
|
83 | graph.get("zuck", function(err, res) {
|
84 | console.log(res); // { id: '4', name: 'Mark Zuckerberg'... }
|
85 | });
|
86 | ```
|
87 |
|
88 | params in the `url`
|
89 |
|
90 | ```js
|
91 | graph.get("zuck?fields=picture", function(err, res) {
|
92 | console.log(res); // { picture: 'http://profile.ak.fbcdn.net/'... }
|
93 | });
|
94 | ```
|
95 |
|
96 | params as an `object`
|
97 |
|
98 | ```js
|
99 | var params = { fields: picture };
|
100 |
|
101 | graph.get("zuck", params, function(err, res) {
|
102 | console.log(res); // { picture: "http://profile.ak.fbcdn.net/..." }
|
103 | });
|
104 | ```
|
105 |
|
106 | GraphApi calls that __redirect__ directly to an image
|
107 | will return a `json` response with relevant fields
|
108 |
|
109 | ```js
|
110 | graph.get("/zuck/picture", function(err, res) {
|
111 | console.log(res); // { image: true, location: "http://profile.ak.fb..." }
|
112 | });
|
113 | ```
|
114 |
|
115 | ## Search data from the Graph Api
|
116 |
|
117 | Search for public posts that contain __brogramming__
|
118 |
|
119 | ```js
|
120 | var searchOptions = {
|
121 | q: "brogramming"
|
122 | , type: "post"
|
123 | };
|
124 |
|
125 | graph.search(searchOptions, function(err, res) {
|
126 | console.log(res); // {data: [{id: xxx, from: ...}, {id: xxx, from: ...}]}
|
127 | });
|
128 | ```
|
129 |
|
130 | ## Publish data to the Graph Api
|
131 | All publish requests will require an `access token`
|
132 |
|
133 | only needs to be set once
|
134 |
|
135 | ```js
|
136 | graph.setAccessToken(accessToken);
|
137 | ```
|
138 |
|
139 | Post a message on a `friend's` wall
|
140 |
|
141 | ```js
|
142 | var wallPost = {
|
143 | message: "I'm gonna come at you like a spider monkey, chip!"
|
144 | };
|
145 |
|
146 | graph.post(userId + "/feed", wallPost, function(err, res) {
|
147 | // returns the post id
|
148 | console.log(res); // { id: xxxxx}
|
149 | });
|
150 | ```
|
151 |
|
152 | ## Delete a Graph object
|
153 |
|
154 | To delete a graph object, provide an `object id` and the
|
155 | response will return `{data: true}` or `{data:false}`
|
156 |
|
157 | ```js
|
158 | graph.del(postID, function(err, res) {
|
159 | console.log(res); // {data:true}/{data:false}
|
160 | });
|
161 | ```
|
162 |
|
163 | ## Performing a FQL query
|
164 |
|
165 | A single FQL query is done by sending a query as a string
|
166 |
|
167 | ```js
|
168 | var query = "SELECT name FROM user WHERE uid = me()";
|
169 |
|
170 | graph.fql(query, function(err, res) {
|
171 | console.log(res); // { data: [ { name: 'Ricky Bobby' } ] }
|
172 | });
|
173 | ```
|
174 |
|
175 | ## Performing a FQL Multi-Query
|
176 |
|
177 | FQL Multi-Queries are done by sending in an object containing the separate queries
|
178 |
|
179 | ```js
|
180 | var query = {
|
181 | name: "SELECT name FROM user WHERE uid = me()"
|
182 | , permissions: "SELECT email, user_about_me, user_birthday FROM permissions WHERE uid = me()"
|
183 | };
|
184 |
|
185 | graph.fql(query, function(err, res) {
|
186 | console.log(res);
|
187 | // { data: [
|
188 | // { name: 'name', fql_result_set: [{name: 'Ricky Bobby'}] },
|
189 | // { name: 'permissions', fql_result_set: [{email: 1, user_about_me: 1...}] }
|
190 | // ]}
|
191 | });
|
192 | ```
|
193 |
|
194 | ## Rockin' it on an Express App
|
195 |
|
196 | This example assumes that you have a link on the main page `/` that points to `/auth/facebook`.
|
197 | The user will click this link and get into the facebook authorization flow ( if the user hasn't already connected)
|
198 | After `authorizing` the app the user will be redirected to `/UserHasLoggedIn`
|
199 |
|
200 | ```js
|
201 | /**
|
202 | * Module dependencies.
|
203 | */
|
204 |
|
205 | var express = require('express')
|
206 | , graph = require('fbgraph')
|
207 | , app = module.exports = express.createServer();
|
208 |
|
209 | // this should really be in a config file!
|
210 | var conf = {
|
211 | client_id: 'YOUR FACEBOOK APP ID'
|
212 | , client_secret: 'YOU FACEBOOK APP SECRET'
|
213 | , scope: 'email, user_about_me, user_birthday, user_location, publish_stream'
|
214 | , redirect_uri: 'http://localhost:3000/auth/facebook'
|
215 | };
|
216 |
|
217 | // Configuration
|
218 |
|
219 | app.configure(function(){
|
220 | app.set('views', __dirname + '/views');
|
221 | app.set('view engine', 'jade');
|
222 | app.use(express.bodyParser());
|
223 | app.use(express.methodOverride());
|
224 | app.use(app.router);
|
225 | app.use(express.static(__dirname + '/public'));
|
226 | });
|
227 |
|
228 | app.configure('development', function(){
|
229 | app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
230 | });
|
231 |
|
232 | app.configure('production', function(){
|
233 | app.use(express.errorHandler());
|
234 | });
|
235 |
|
236 | // Routes
|
237 |
|
238 | app.get('/', function(req, res){
|
239 | res.render("index", { title: "click link to connect" });
|
240 | });
|
241 |
|
242 | app.get('/auth/facebook', function(req, res) {
|
243 |
|
244 | // we don't have a code yet
|
245 | // so we'll redirect to the oauth dialog
|
246 | if (!req.query.code) {
|
247 | var authUrl = graph.getOauthUrl({
|
248 | "client_id": conf.client_id
|
249 | , "redirect_uri": conf.redirect_uri
|
250 | });
|
251 |
|
252 | if (!req.query.error) { //checks whether a user denied the app facebook login/permissions
|
253 | res.redirect(authUrl);
|
254 | } else { //req.query.error == 'access_denied'
|
255 | res.send('access denied');
|
256 | }
|
257 | return;
|
258 | }
|
259 |
|
260 | // code is set
|
261 | // we'll send that and get the access token
|
262 | graph.authorize({
|
263 | "client_id": conf.client_id
|
264 | , "redirect_uri": conf.redirect_uri
|
265 | , "client_secret": conf.client_secret
|
266 | , "code": req.query.code
|
267 | }, function (err, facebookRes) {
|
268 | res.redirect('/loggedIn');
|
269 | });
|
270 |
|
271 |
|
272 | });
|
273 |
|
274 |
|
275 | // user gets sent here after being authorized
|
276 | app.get('/UserHasLoggedIn', function(req, res) {
|
277 | res.render("index", { title: "Logged In" });
|
278 | });
|
279 |
|
280 |
|
281 | var port = process.env.PORT || 3000;
|
282 | app.listen(port, function() {
|
283 | console.log("Express server listening on port %d", port);
|
284 | });
|
285 |
|
286 | ```
|
287 |
|
288 | ## Running tests
|
289 |
|
290 | Before running the test suite, add your Facebook `appId` and `appSecret` to `tests/config.js`
|
291 | This is needed to create `test users` and to get a test `access_token`
|
292 |
|
293 | $ npm install
|
294 | $ make test
|
295 |
|
296 | _Tests might fail if the Facebook api has an issue._
|
297 |
|
298 | ## License
|
299 |
|
300 | (The MIT License)
|
301 |
|
302 | Copyright (c) 2011 Cristiano Oliveira <ocean.cris@gmail.com>
|
303 |
|
304 | Permission is hereby granted, free of charge, to any person obtaining
|
305 | a copy of this software and associated documentation files (the
|
306 | 'Software'), to deal in the Software without restriction, including
|
307 | without limitation the rights to use, copy, modify, merge, publish,
|
308 | distribute, sublicense, and/or sell copies of the Software, and to
|
309 | permit persons to whom the Software is furnished to do so, subject to
|
310 | the following conditions:
|
311 |
|
312 | The above copyright notice and this permission notice shall be
|
313 | included in all copies or substantial portions of the Software.
|
314 |
|
315 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
316 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
317 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
318 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
319 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
320 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
321 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
322 |
|