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?
Asked
Active
Viewed 1,301 times
4

Community
- 1
- 1

Can Poyrazoğlu
- 33,241
- 48
- 191
- 389
-
Have you had any luck on this? – Rambatino Nov 13 '14 at 20:29
-
@Rambatino I've added my "solution" as an answer. – Can Poyrazoğlu Nov 13 '14 at 21:41
1 Answers
2
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.

Can Poyrazoğlu
- 33,241
- 48
- 191
- 389
-
1I literally just used this method like an hour after i commented! Great stuff, works too! Although users will most likely be on v2.2 now :). Also, if anyone is puzzled about authData even after include i still needed to fetch() the user to actually have a defined authData field :s – Rambatino Nov 14 '14 at 01:50
-
This might be a bit late but I've been trying to use this solution to no avail. The access token is not accepted and the graph API returns an error saying the access token is not active. Did you come across that issue? – BarakChamo Jan 13 '15 at 19:18
-
-
@fatuhoku try `user.get('authData')['facebook']['access_token'];` – Can Poyrazoğlu Dec 03 '15 at 12:27