-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.js
49 lines (39 loc) · 1.09 KB
/
facebook.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
var querystring = require('querystring');
var https = require('https');
/*
fb object:
{
appId : your_app_id,
redirectURI : the_uri_you_ask_facebook_to_redirect_to,
appSecret : your_app_secret,
oauthHOST : 'graph.facebook.com'
}
*/
//callback parameters: error, facebookUserJson
exports.getFacebookUser = function(fb, accessToken, callback){
var tokenURL = '/oauth/access_token?' +
'client_id=' + fb.appId +
'&redirect_uri=' + querystring.escape(fb.redirectURI) +
'&client_secret=' + fb.appSecret +
'&code=' + accessToken;
var options = {
host: fb.oauthHOST,
path: tokenURL
}
https.get(options, function(oauthRes){
var facebookData = '';
oauthRes.on('data', function(data){
facebookData += data;
});
oauthRes.on('end', function(){
var graphURL = '/me?' + facebookData;
https.get({host: fb.oauthHOST, path: graphURL }, function(graphResponse){
graphResponse.on('data', function(data){
callback(null, JSON.parse(data));
});
});
});
}).on('error', function(e){
callback(e);
});
}