How to access Facebook Graph API in Parse Cloud Code?

烂漫一生 提交于 2019-12-07 08:25:01

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!