-
-
Save jaaaco/b3a5adee675772e5ff68ee6b0cc28f67 to your computer and use it in GitHub Desktop.
This file contains 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
class FB { | |
constructor() { | |
this.version = 'v2.9'; | |
this.credentials = { | |
appId: process.env.FACEBOOK_APP_ID, | |
secret: process.env.FACEBOOK_APP_SECRET, | |
}; | |
this.redirectUrl = process.env.FACEBOOK_REDIRECT_URL || 'http://localhost:3000/facebook-callback'; | |
} | |
call(method, params = {}) { | |
return new Promise((resolve, reject) => { | |
let url = `https://graph.facebook.com/${this.version}/${method}?client_id=${this.credentials.appId}&redirect_uri=${encodeURIComponent(this.redirectUrl)}&client_secret=${encodeURIComponent(this.credentials.secret)}`; | |
_.keys(params).forEach(key => { | |
url += `&${key}=${encodeURIComponent(params[key])}`; | |
}); | |
https.get(url, (res) => { | |
let data = ''; | |
res.on('data', (d) => { | |
data += d; | |
}); | |
res.on('end', () => { | |
data = JSON.parse(data); | |
if (res.statusCode !== 200) { | |
reject(data); | |
} else { | |
resolve(data); | |
} | |
}); | |
}).on('error', (e) => { | |
console.error(e); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment