如何使用图形API获取Facebook用户的电子邮件地址? [英] How to get the email address of a Facebook user using the graph API?

查看:117
本文介绍了如何使用图形API获取Facebook用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这篇文章 ,但这对我来说没有意义。

I have read through this post over and over, and yet it doesn't make sense to me.

我想要做的是获取Facebook用户的电子邮件地址,以便我可以将其存储在字符串中比较看看它是否与MySQL数据库中现有用户的电子邮件相匹配。当用户首次授予应用程序的访问权限时,我只需要执行此操作。

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.

在我的应用开发人员界面的Authenticated Referrals部分,我设置了用户和朋友权限包含电子邮件。

In the "Authenticated Referrals" section of my app developer interface, I have set "User & Friend Permissions" to include "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;
    }
}

如果我的var_dup()$ user,我可以看到用户ID(虽然没有太多的其他),这似乎是正确的,所以我相信我正在Facebook上正确的地方连接。

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.

但是, HREF =https://stackoverflow.com/questions/3611682/facebook-graph-api-how-to-get-users-email#comment3792810_3611781>此建议的路线码的,这意味着它返回一个电子邮件地址,但是当我回覆它看看它的样子,它不会返回一个电子邮件地址:

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.

然后我也碰到了一个这篇文章,其中有代码混合以HTML的方式让我更进一步。

And then I also came across this post, which has code mixed in with the HTML in a way that baffles me even further.

假设我的用户已经授权我的应用查看他们的电子邮件,是不是有一个简单的方法得到它吗像我这样没有经验的程序员可以掌握吗?

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?

推荐答案

你使用的是过时的方法。您可以使用以下代码检索电子邮件。

You are using outdated method. You can use the code below to retrieve the email.

您可以从以下网址找到有关Facebook Connect的更多信息: http://25labs.com/tutorial- integration-facebook-connect-to-your-website-using-php-sdk-v-3-xx-which-uses-graph-api /

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/

$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'];
}

这篇关于如何使用图形API获取Facebook用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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