-
Notifications
You must be signed in to change notification settings - Fork 177
/
graph.js
562 lines (452 loc) · 12.5 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/**
* Module Dependencies
*/
var request = require('request')
, qs = require('qs')
, url = require('url')
, crypto = require('crypto')
, noop = function(){};
// Using `extend` from https://github.com/Raynos/xtend
function extend(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i]
, keys = Object.keys(source);
for (var j = 0; j < keys.length; j++) {
var name = keys[j];
target[name] = source[name];
}
}
return target;
}
/**
* @private
*/
var accessToken = null
, appSecret = null
, graphUrl = 'https://graph.facebook.com'
, graphVersion = '2.9' // default to the oldest version
, oauthDialogUrl = "https://www.facebook.com/v2.0/dialog/oauth?" // oldest version for auth
, oauthDialogUrlMobile = "https://m.facebook.com/v2.0/dialog/oauth?" // oldest version for auth
, requestOptions = {};
/**
* Library version
*/
exports.version = '1.3.0';
/**
* Graph Stream
*
* @param {String} method
* @param {String} url
* @param {object/function} - postData
* - object to be used for post
* - assumed to be a callback function if callback is undefined
* @param {function/undefined} - callback function
*/
function Graph(method, url, postData, callback) {
if (typeof callback === 'undefined') {
callback = postData;
postData = {};
}
url = this.prepareUrl(url);
this.callback = callback || noop;
this.postData = postData;
this.options = extend({}, requestOptions);
this.options.encoding = this.options.encoding || 'utf-8';
// these particular set of options should be immutable
this.options.method = method;
this.options.uri = url;
this.options.followRedirect = false;
this.request = this[method.toLowerCase()]();
return this;
}
/**
* "Prepares" given url string
* - adds protocol and host prefix if none is given
* @param {string} url string
*/
Graph.prototype.prepareUrl = function(url) {
url = this.cleanUrl(url);
if (url.substr(0,4) !== 'http') {
url = graphUrl + '/v' + graphVersion + url;
}
return url;
};
/**
* "Cleans" given url string
* - adds lading slash
* - adds access token if we have one
* - adds appsecret_proof if we have an accessToken and appSecret
* @param {string} url string
*/
Graph.prototype.cleanUrl = function(url) {
url = url.trim();
// prep access token in url for appsecret proofing
var regex = /access_token=([^&]*)/;
var results = regex.exec(url);
var sessionAccessToken = results ? results[1] : accessToken;
// add leading slash
if (url.charAt(0) !== '/' && url.substr(0,4) !== 'http') url = '/' + url;
// add access token to url
if (accessToken && url.indexOf('access_token=') === -1) {
url += ~url.indexOf('?') ? '&' : '?';
url += "access_token=" + accessToken;
}
// add appsecret_proof to the url
if (sessionAccessToken && appSecret && url.indexOf('appsecret_proof') === -1) {
var hmac = crypto.createHmac('sha256', appSecret);
hmac.update(sessionAccessToken);
url += ~url.indexOf('?') ? '&' : '?';
url += "appsecret_proof=" + hmac.digest('hex');
}
return url;
};
/**
* Gets called on response.end
* @param {String|Object} body
*/
Graph.prototype.end = function (body) {
var json = typeof body === 'string' ? null : body
, err = null;
if (!json) {
try {
// this accounts for `real` json strings
if (~body.indexOf('{') && ~body.indexOf('}')) {
json = JSON.parse(body);
} else {
// this accounts for responses that are plain strings
// access token responses have format of "accessToken=....&..."
// but facebook has random responses that just return "true"
// so we'll convert those to { data: true }
if (!~body.indexOf('=')) body = 'data=' + body;
if (body.charAt(0) !== '?') body = '?' + body;
json = url.parse(body, true).query;
}
} catch (e) {
err = {
message: 'Error parsing json'
, exception: e
};
}
}
if (!err && (json && json.error)) err = json.error;
this.callback(err, json);
};
/**
* https.get request wrapper
*/
Graph.prototype.get = function () {
var self = this;
return request.get(this.options, function(err, res, body) {
if (err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
return;
}
if (~res.headers['content-type'].indexOf('image')) {
body = {
image: true
, location: res.headers.location
};
}
self.end(body);
}).on('error', function(err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
});
};
/**
* https.post request wrapper
*/
Graph.prototype.post = function() {
var self = this
, postData = qs.stringify(this.postData);
this.options.body = postData;
return request(this.options, function (err, res, body) {
if (err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
return;
}
self.end(body);
})
.on('error', function(err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
});
};
/**
* Accepts an url an returns facebook
* json data to the callback provided
*
* if the response is an image
* ( FB redirects profile image requests directly to the image )
* We'll send back json containing {image: true, location: imageLocation }
*
* Ex:
*
* Passing params directly in the url
*
* graph.get("zuck?fields=picture", callback)
*
* OR
*
* var params = { fields: picture };
* graph.get("zuck", params, callback);
*
* GraphApi calls that redirect directly to an image
* will return a json response with relavant fields
*
* graph.get("/zuck/picture", callback);
*
* {
* image: true,
* location: "http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg"
* }
*
*
* @param {object} params
* @param {string} url
* @param {function} callback
*/
exports.get = function(url, params, callback) {
if (typeof params === 'function') {
callback = params;
params = null;
}
if (typeof url !== 'string') {
return callback({ message: 'Graph api url must be a string' }, null);
}
if (params) {
url += ~url.indexOf('?') ? '&' : '?';
url += qs.stringify(params);
}
return new Graph('GET', url, callback);
};
/**
* Publish to the facebook graph
* access token will be needed for posts
* Ex:
*
* var wallPost = { message: "heyooo budday" };
* graph.post(friendID + "/feed", wallPost, callback);
*
* @param {string} url
* @param {object} postData
* @param {function} callback
*/
exports.post = function (url, postData, callback) {
if (typeof url !== 'string') {
return callback({ message: 'Graph api url must be a string' }, null);
}
if (typeof postData === 'function') {
callback = postData;
postData = url.indexOf('access_token') !== -1 ? {} : {access_token: accessToken};
}
return new Graph('POST', url, postData, callback);
};
/**
* Deletes an object from the graph api
* by sending a "DELETE", which is really
* a post call, along with a method=delete param
*
* @param {string} url
* @param {object} postData (optional)
* @param {function} callback
*/
exports.del = function (url, postData, callback) {
if (!url.match(/[?|&]method=delete/i)) {
url += ~url.indexOf('?') ? '&' : '?';
url += 'method=delete';
}
if (typeof postData === 'function') {
callback = postData;
postData = url.indexOf('access_token') !== -1 ? {} : {access_token: accessToken};
}
return this.post(url, postData, callback);
};
/**
* Perform a search on the graph api
*
* @param {object} options (search options)
* @param {function} callback
*/
exports.search = function (options, callback) {
options = options || {};
var url = '/search?' + qs.stringify(options);
return this.get(url, callback);
};
/**
* Perform a batch query on the graph api
*
* @param {Array} reqs An array containing queries
* @param {[Object]} additionalData Additional data to send, e.g. attachments or the `include_headers` parameter.
* @param {Function} callback
*
* @see https://developers.facebook.com/docs/graph-api/making-multiple-requests
*/
exports.batch = function (reqs, additionalData, callback) {
if (!(reqs instanceof Array)) {
return callback({ message: 'Graph api batch requests must be an array' }, null);
}
if (typeof additionalData === 'function') {
callback = additionalData;
additionalData = {};
}
return new Graph('POST', '', extend({}, {
access_token: accessToken,
batch: JSON.stringify(reqs)
}, additionalData), callback);
};
/**
* Perform a fql query or mutliquery
* multiqueries are done by sending in
* an object :
*
* var query = {
* name: "SELECT name FROM user WHERE uid = me()"
* , permissions: "SELECT " + FBConfig.scope + " FROM permissions WHERE uid = me()"
* };
*
* @param {string/object} query
* @param {object} params
* @param {function} callback
*/
exports.fql = function (query, params, callback) {
if (typeof query !== 'string') query = JSON.stringify(query);
var url = '/fql?q=' + encodeURIComponent(query);
if (typeof params === 'function') {
callback = params;
params = null;
return this.get(url, callback);
} else {
return this.get(url, params, callback);
}
};
/**
* @param {object} params containing:
* - client_id
* - redirect_uri
* @param {object} opts Options hash. { mobile: true } will return mobile oAuth URL
* @returns the oAuthDialogUrl based on params
*/
exports.getOauthUrl = function (params, opts) {
var url = (opts && opts.mobile) ? oauthDialogUrlMobile : oauthDialogUrl;
return url + qs.stringify(params);
};
/**
* Authorizes user and sets the
* accessToken if everything worked out
*
* @param {object} params containing:
* - client_id
* - redirect_uri
* - client_secret
* - code
* @param {function} callback
*/
exports.authorize = function (params, callback) {
var self = this;
return this.get("/oauth/access_token", params, function(err, res) {
if (!err) self.setAccessToken(res.access_token);
callback(err, res);
});
};
/**
* Extends the expiration time of accessToken
*
* @param {object} params containing:
* - client_id
* - client_secret
* - access_token (optional)
* @param {function} callback
*/
exports.extendAccessToken = function (params, callback) {
var self = this;
params.grant_type = 'fb_exchange_token';
params.fb_exchange_token = params.access_token ? params.access_token : this.getAccessToken();
return this.get("/oauth/access_token", params, function(err, res) {
if (!err && !params.access_token) {
self.setAccessToken(res.access_token);
}
callback(err, res);
});
};
/**
* Set request options.
* These are mapped directly to the
* `request` module options object
* @param {Object} options
*/
exports.setOptions = function (options) {
if (typeof options === 'object') requestOptions = options;
return this;
};
/**
* @returns the request options object
*/
exports.getOptions = function() {
return requestOptions;
};
/**
* Sets the access token
* @param {string} token
*/
exports.setAccessToken = function(token) {
accessToken = token;
return this;
};
/**
* @returns the access token
*/
exports.getAccessToken = function () {
return accessToken;
};
/**
* Set's the Graph API version.
* Note that you don't need to specify the 'v', just
* add '2.1', '1.1' etc
* @param {string} version
*/
exports.setVersion = function (version) {
// set version
graphVersion = version;
// update auth urls
oauthDialogUrl = "https://www.facebook.com/v"+version+"/dialog/oauth?"; // oldest version for auth
oauthDialogUrlMobile = "https://m.facebook.com/v"+version+"/dialog/oauth?"; // oldest version for auth
return this;
};
/**
* Sets the app secret, used to verify all API calls if provided
* @param {string} token
*/
exports.setAppSecret = function(token) {
appSecret = token;
return this;
};
/**
* @returns the app secret
*/
exports.getAppSecret = function () {
return appSecret;
};
/**
* sets graph url
*/
exports.setGraphUrl = function (url) {
graphUrl = url;
return this;
};
/**
* @returns the graphUrl
*/
exports.getGraphUrl = function() {
return graphUrl;
};