I need to get friends of a user that logged in with Facebook at my Parse app, inside a cloud function. How can I achieve this? I've tried to install facebook-node-sdk
(https://github.com/Thuzi/facebook-node-sdk/), but even though I've copied all the dependencies into the same folder, it complains about being unable to load the package when I require
it. I've also found this question: How can I get the user first name from facebook in cloud code? but the answer there doesn't work (FB
is not defined). I can do raw REST calls to Graph API with my access token using Parse.Cloud.httpRequest
but managing endpoints, access token, parameters etc. quickly becomes messy. What is the preferred way of accessing Facebook Graph API from Parse Cloud Code?
I couldn't use any libraries, so I've ended up using plain HTTP REST requests. For example, I was trying to get a user's name, gender, and user ID, I used this code by manually constructing the URL (where I had token
access token variable of the user:
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.1/me?fields=id,gender,name&access_token=' + token,
success: function(httpResponse) {
var responseData = httpResponse.data;
...
where responseData
is now a JSON object with the returned data. It's not the best thing to do, but it works great for simple tasks.
来源:https://stackoverflow.com/questions/26617690/how-to-access-facebook-graph-api-in-parse-cloud-code