如何在Parse Cloud Code中访问Facebook Graph API? [英] How to access Facebook Graph API in Parse Cloud Code?

查看:127
本文介绍了如何在Parse Cloud Code中访问Facebook Graph API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Parse应用程序中,在云端功能中获取与Facebook登录的用户的朋友。我该如何实现?我试图安装 facebook-node-sdk https://github.com/Thuzi/facebook-node-sdk/ ),但即使我将所有依赖项复制到同一个文件夹中,它抱怨无法加载该包当我需要它。我也发现了这个问题:如何从Facebook的云代码中获取用户的名字?但是答案不起作用( FB 未定义)。我可以使用 Parse.Cloud.httpRequest 使用我的访问令牌对Graph API进行原始REST调用,但是管理端点,访问令牌,参数等会很快变得凌乱。从Parse Cloud Code访问Facebook Graph API的首选方法是什么?

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?

推荐答案

我无法使用任何库,所以我最终使用纯HTTP HTTP请求。例如,我正在尝试获取用户的姓名,性别和用户ID,我通过手动构建URL(其中我有令牌访问令牌变量用户:

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;
     ...

其中 responseData 现在是一个JSON对象,返回的数据,这不是最好的事情,但它对于简单的任务非常有用。

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.

这篇关于如何在Parse Cloud Code中访问Facebook Graph API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆