php 如何使用图形 API 获取 Facebook 用户的电子邮件地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9767949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the email address of a Facebook user using the graph API?
提问by Questioner
I have read through this postover and over, and yet it doesn't make sense to me.
我一遍又一遍地阅读这篇文章,但对我来说没有意义。
What I am trying to do is acquire a Facebook user's email address so that I can store it in a string and compare to see if it matches the emails of existing users in my MySQL database. I only need to to do this when the user first grants access rights to the app.
我想要做的是获取 Facebook 用户的电子邮件地址,以便我可以将其存储在一个字符串中并进行比较以查看它是否与我的 MySQL 数据库中现有用户的电子邮件匹配。我只需要在用户首次授予应用程序访问权限时执行此操作。
In the "Authenticated Referrals" section of my app developer interface, I have set "User & Friend Permissions" to include "email".
在我的应用程序开发人员界面的“Authenticated Referrals”部分,我设置了“User & Friend Permissions”以包含“email”。
Within the "canvas" area of my site, I have this code:
在我网站的“画布”区域中,我有以下代码:
include('facebook/base_facebook.php');
include('facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => "XXXXXXXXXXXXXXX",
'secret' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
));
$user = $facebook->getUser();
if ($user)
{
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
If I var_dup() $user, I can see the user id (though not much else) which seems to be correct, so I believe I'm connecting to the right place on Facebook.
如果我使用 var_dup() $user,我可以看到似乎正确的用户 ID(虽然没有其他太多),所以我相信我正在连接到 Facebook 上的正确位置。
But then there was this suggested line of code, which implies that it returns an email address, but when I echo it out to see what it looks like, it doesn't return an email address:
但是后来有这个建议的代码行,这意味着它返回一个电子邮件地址,但是当我回显它以查看它的样子时,它没有返回一个电子邮件地址:
echo "<br/>" . $facebook->getLoginUrl(array('req_perms' => 'email'));
I kind of thought it wouldn't, as it doesn't look quite right to me, but on the other hand, I just have no idea what else to do with it or where to put it.
我有点认为它不会,因为它对我来说看起来不太合适,但另一方面,我只是不知道还能用它做什么或把它放在哪里。
And then I also came across this post, which has code mixed in with the HTML in a way that baffles me even further.
然后我还看到了这篇文章,其中将代码与 HTML 混合在一起,这让我更加困惑。
Assuming that my user has granted permission for my app to see their email, isn't there a simple way of getting at it? Something an inexperienced programmer like myself can grasp?
假设我的用户已授予我的应用程序查看其电子邮件的权限,难道没有一种简单的方法可以获取它吗?像我这样没有经验的程序员可以掌握的东西吗?
采纳答案by Tebe Tensing
You are using outdated method. You can use the code below to retrieve the email.
您正在使用过时的方法。您可以使用下面的代码来检索电子邮件。
You can find more information about Facebook Connect from : http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/
您可以从以下位置找到有关 Facebook Connect 的更多信息:http: //25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-xx-which-uses-graph-接口/
$app_id = "xxxxxxxxx";
$app_secret = "xxxxxxxxxx";
$site_url = "xxxxxxxxxxxxx";
include_once "src/facebook.php";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
回答by Juicy Scripter
As suggested in comments you should replace req_perms
with scope
since it is the name for field indicating which permissions
to be granted by users.
正如评论中所建议的,您应该替换为req_perms
,scope
因为它是指示permissions
用户授予的字段的名称。
Once email
permission is granted (do you see it's asked in the Auth Dialog?) you'll be able to get email
field via Graph API. You can also retrieve only required fields in results by specifying 'em in the fields
parameter:
一旦email
授予权限(你看到它的要求在验证对话框?)你就可以得到email
通过图形API领域。您还可以通过在fields
参数中指定 'em 来仅检索结果中的必填字段:
$facebook->api('/me', array('fields' => 'id,email'));
BTW, Please not that both posts you refer to are old (2010), way many things changed in the platform. Be sure to read updated documentation on Permissionsand Authentication
顺便说一句,请注意,您所引用的两篇文章都是旧的(2010 年),平台中的许多事情都发生了变化。请务必阅读有关权限和身份验证的更新文档
回答by Umair Hamid
You can concatenate a Facebook username with the e-mail address. For example, my username is "umair.hamid.79" on Facebook. It will be "[email protected]".
您可以将 Facebook 用户名与电子邮件地址连接起来。例如,我在 Facebook 上的用户名是“umair.hamid.79”。它将是“[email protected]”。