如何将Facebook朋友导入网站 [英] How to import Facebook friends to website

查看:65
本文介绍了如何将Facebook朋友导入网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有用户的网站,用户可以将彼此添加为朋友.最近,我看到了很多具有功能的Web应用程序,您可以将您的朋友从Facebook直接导入他们的Web应用程序.如何导入Facebook朋友列表的这种类型?

I've got a website that has users and users can add each other as friends. Lately, I've been seeing a lot of web apps that have a function where you can import your friends from Facebook directly into their web app. How is this type of import of the Facebook friend list implemented?

我查看了Facebook API文档,但实际上并不能直接找到任何指向此的东西.

I looked in the Facebook API docs, and can't really find anything directly on point to this.

大概我会使用Facebook API提取用户朋友列表.从那里,我将如何在我的网站上找到该列表的成员?这种类型的方案是否只能与已经使用Facebook朋友搜索过的其他用户一起使用,以便您可以将Facebook帐户绑定到他们?

Presumably I would use the Facebook API to pull a list of the user's friends. From there, how would I then find users on my site that are members of that list? Does this type of scheme only work with other users that have already used the Facebook friend search themselves, so that you can tie a Facebook account to them?

尽管我看到很多网站都在做这个话题,但实际上似乎没有太多建议,因此,无论多么基础,任何帮助都将不胜感激.

There really doesn't seem to be much out there on this topic, even though I've seen a lot of sites doing it, so any help, no matter how basic, would be much appreciated.

推荐答案

该中心所需的基础知识图形API .

The basics of what you'll need for this center around registering your site as "a Facebook application" and making use of the Graph API.

查看此页面的使用Facebook登录"部分.基本上,当您注册应用程序"时,您将拥有一个应用程序ID和一个应用程序密钥"(这只是一个字符串,并且不得公开,否则任何带有它的人都可以作为您的应用程序进行欺骗).页面上的示例显示了少量JavaScript,用于从您的网站初始化应用程序,此时,您可以进行Facebook集成了.

Take a look at the "Login with Facebook" section of this page. Basically, when you register your "application" you will have an application ID and an "application secret" (which is just a string of characters, and must never be made public or anybody with it could spoof as your application). The example on the page shows a small amount of JavaScript to initialize the application from your website, at which point you're good to go for Facebook integration.

下面仅是使用Facebook登录"按钮的示例,您的用户将用来表示他们希望允许您的网站访问其一些Facebook数据(例如,朋友"列表).您可以在该登录标记中高度自定义您的网站请求用户的特定权限,并且系统会向您的用户显示此信息这样的提示,专门要求您允许网站访问访问他们的Facebook数据.

Just below that is an example of the "Login with Facebook" button, which is what your users would use to indicate that they wish to allow your site to access some of their Facebook data (the Friends list, for example). You can highly customize the specific permissions your site requests from the user within that login tag, and your users would be presented with a prompt like this one specifically asking for permission to allow your site to access their Facebook data.

再次回顾此页面(尽管它可能在其他页面好吧,Facebook文档发生了很大变化),再向下滚动一点,您会发现一个简单的示例(在PHP中),用于非常基本的Facebook集成.该示例页面向用户显示使用Facebook登录"按钮,并在单击按钮后重新加载自身(通过订阅JavaScript中的正确Facebook API事件).重新加载后,用户现在拥有一个加密的cookie(假定用户同意许可请求),示例页面可以读取该cookie.

Looking back at this page again (though it may be on other pages as well, the Facebook documentation changes a lot), scrolling down a little further you find a simple example (in PHP) for very basic Facebook integration. The example page presents the user with a "Login with Facebook" button and reloads itself after the button is clicked (by subscribing to the correct Facebook API event in JavaScript). Upon reload, the user now has an encrypted cookie (assuming the user agreed to the permissions request) which the example page can read.

应用程序机密"用于解密cookie,该cookie包含用户的Facebook ID和访问令牌,该令牌授权您的网站向该用户的Facebook信息发出请求.在Graph API请求中使用访问令牌将授予您从这些请求接收数据的权限.

The "application secret" is used to decrypt the cookie, which contains the user's Facebook ID and the access token which authorizes your site to make requests for that user's Facebook information. Using the access token in Graph API requests will grant you permission to receive the data from those requests.

Graph API请求只是返回JSON数据的GET请求,因此一旦您具有访问令牌(和用户ID作为起点),便可以在任何需要的地方发出这样的请求.可以使用JavaScript(以及使用社交插件和其他Facebook小部件)在客户端中将它们设置为"Facebookify"用户在您网站上的体验,而不会向您的网站暴露太多(如果有的话)数据或责任.或者,可以从后端的服务器端代码发出请求,以存储请求的数据,并在您的网站认为合适时使用它(例如,朋友"列表).

Graph API requests are just GET requests which return JSON data, so once you have the access token (and the user ID as a starting point) you can make such requests anywhere you want. They can be made client-side in JavaScript (along with using Social Plugins and other Facebook widgets) to "Facebookify" your users' experience on your site without exposing much, if any, data or responsibility to your site. Or the requests can be made from your server-side code on the back end for the purpose of storing requested data and using it as your site sees fit (such as the Friends list).

获取好友列表的API请求将如下所示:

An API request to get a list of Friends would be something like this:

https://graph.facebook.com/me/friends?access_token=your_access_token

(但是,您将路径中的me替换为您要检索其好友的用户的Facebook ID,并用实际的访问令牌替换your_access_token.示例链接为您自己的帐户,假设您已登录Facebook,可以在此页面上找到.)

(However, you'd replace the me in the path with the Facebook ID of the user whose Friends you wish to retrieve, and replace your_access_token with the actual access token. Example links for your own account, assuming you're signed in to Facebook, can be found on this page.)

Graph API(顾名思义)在Facebook数据的社交图上向外扩展.可以在其自己的API请求中请求带有id返回的任何JSON对象,以查看其更多信息.您想要的任何信息,例如用户的好友的电子邮件地址,都将在 if 和(希望的) 用户和用户的权限均可用有问题的朋友允许这样做. (很抱歉,但是如果用户的朋友明确表示他们的朋友无法授予对此信息的访问权限,则该信息不可用.)

The Graph API (as the name suggests) extends outward across the social graph of Facebook data. Any JSON object which comes back with an id can be requested in its own API request to see more of its information. Any piece of information you want, such as the email addresses of the user's Friends, will be available if and (hopefully) only if the permissions of both the user and the user's Friend in question allows this. (Sorry, but if the user's Friends have specifically said that their Friends can't grant access to this information, then it's not available.)

因此,一旦您的用户向您的网站授予了请求的查看其Facebook数据的权限,您就可以收获Graph API中可用的所需(并被允许访问)的任何数据. (我讨厌在那里使用收获"一词,但您会明白.当然,要负责任地采取行动.)因此,在任何给定实例中,从用户的角度来看,您的过程将是:

So, once your users grant your site the requested permissions to view their Facebook data, you can harvest whatever data you want (and are allowed to access) that's available in the Graph API. (I hate using the word "harvest" there, but you get the idea. Act responsibly, of course.) So your process, from the perspective of the user in any given instance, would be:

  1. 要求用户使用Facebook登录,请求特定权限.
  2. 从用户那里接收那些权限(否则该过程将在此处结束).
  3. 从您的服务器代码发出后端Graph API请求,以从用户的Facebook社交图获取相关信息.
  4. 为用户提供可能有意义的选项(例如,将电子邮件地址甚至姓名与已经拥有的用户进行匹配),无论是已经拥有的用户(嘿,您的这个朋友在我们的网站上,想要打招呼吗?)或邀请您访问您的网站(我们没有您的朋友访问我们网站的记录,请单击此处邀请他们!)等等.
  5. 然后,用户继续与您的网站在此问题上提供的任何功能进行交互.

有道理吗?正如我所提到的,Facebook上所有这些文档的文档都发生了很大变化(在编写此文档时,我发现自己站点中的几个无效链接).它们的编程接口也经常更改.他们可能会开发出更简单的方法来进行此操作,但是一旦您设置好并开始进行Graph API调用以及所有这些操作的抽象之后,将您的网站与Facebook集成起来就变得非常简单.

Make sense? As I mentioned, the documentation for all of this on Facebook changes a lot (I found several dead links within their own site while writing this). Their programmatic interfaces change fairly often as well. They may develop easier ways to do this, but once you get set up and going and abstract out the Graph API calls and all that, it becomes pretty straightforward to integrate your site(s) with Facebook.

这篇关于如何将Facebook朋友导入网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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