Created
May 19, 2016 08:39
-
-
Save jesusprubio/de34179daadcc37e7be1ac14ed950123 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get an user Facebook friends. It accepts an ETag parameter to avoid | |
// non-changed ones to be returned again. | |
// https://developers.facebook.com/docs/marketing-api/etags | |
const https = require('https'); | |
// Credentials | |
const app_id = 'X'; | |
const app_secret = 'Y'; | |
const user_token = 'Z'; | |
// Request options. | |
const opts = { | |
hostname: "graph.facebook.com", | |
path: `/me/friends?access_token=${user_token}`, | |
// path: "/v2.3/me/friends?access_token=#{user_token}", | |
// path: "/v2.4/me/friends?access_token=#{user_token}", | |
// path: "/v2.5/me/friends?access_token=#{user_token}", | |
// path: "/v2.6/me/friends?access_token=#{user_token}", | |
method: 'GET', | |
headers: { 'User-Agent': '3nder' } | |
}; | |
function handleError(err) { | |
console.error('Error'); | |
console.error(err); | |
process.exit(1); | |
} | |
// Making the first request. | |
const req = https.get(opts, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
console.log('Result 0:'); | |
console.dir(JSON.parse(chunk), { depth: null, colors: true }); | |
}); | |
res.on('end', function() { | |
// Adding the received ETag headers to receive only the data | |
// which have changed since the last check. | |
// https://developers.facebook.com/docs/marketing-api/etags | |
opts.headers['If-None-Match'] = res.headers.etag; | |
console.log(`ETag header added: ${res.headers.etag} to the next request.`); | |
const req1 = https.get(opts, function(res1) { | |
res1.setEncoding('utf8'); | |
res1.on('data', function(chunk) { | |
console.log('Result 1, should include new friends or be empty:'); | |
console.dir(JSON.parse(chunk), { depth: null, colors: true }); | |
}); | |
res1.on('end', () => console.log('Done 1.')); | |
}); | |
return req1.on('error', handleError); | |
}); | |
}); | |
req.on('error', handleError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment