Facebook用户图表不返回电子邮件PHP

Facebook用户图表不返回电子邮件PHP

本文介绍了Facebook用户图表不返回电子邮件PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的Facebook API新手。
我正在为用户创建一个登录页面,其想法是他们使用Facebook登录。



在登录页面的顶部,我有以下代码:

  $ fb = new Facebook\Facebook([
'app_id'=>$ fb_appid,
'app_secret'=>$ fb_appsecret,
'default_graph_version'=>'v2.2',
]);

然后一点点我有登录按钮:


$ b $如果(isset($ fb)){
$ helper = $ fb-> getRedirectLoginHelper(); b

  
$ permissions = ['email']; //可选
$ fb_login_url = $ helper-> getLoginUrl($ server_url / public / facebook / login-callback,$ permissions);
} else $ fb_login_url =;

if(strlen($ fb_login_url)> = 1)echo< a href ='$ fb_login_url'>< i class ='fa fa-2x fa-face-square'> ;< / I>< / A>中;
else echo< a href ='#'disabled ='disabled'>< i class ='fa fa-2x fa-face-square'>< / i>< / a> ;

代码工作直到那里,如果我点击按钮,我会进入Facebook门户,单击允许的权限和任何内容,然后将其重定向到 login-callback



code> login-callback 是:

  if(isset($ fb)){ 
$ helper = $ fb-> getRedirectLoginHelper();
try {
$ accessToken = $ helper-> getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $ e){
$ err_message ='图返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
} catch(Facebook\Exceptions\FacebookSDKException $ e){
$ err_message ='Facebook SDK返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
}

if(strlen($ err_message)< = 0&& isset($ accessToken)){
$ _SESSION ['facebook_access_token'] =(string) $的accessToken;
$ fb-> setDefaultAccessToken($ accessToken);

try {
$ response = $ fb-> get('/ me');
$ userNode = $ response-> getGraphUser();

// $ username = $ userNode-> getName();
// $ firstname = $ userNode-> getFirstName();
// $ lastname = $ userNode-> getLastName();
print_r($ userNode);
} catch(Facebook\Exceptions\FacebookResponseException $ e){
$ err_message ='图返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
} catch(Facebook\Exceptions\FacebookSDKException $ e){
$ err_message ='Facebook SDK返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
}
}
}

$ userNode 看起来像这样:

  Facebook\GraphNodes\GraphUser Object 

[items:protected] => Array

[name] => Jacques Koekemoer
[id] => xxxxxxxxxxxxxxxxx



我已设置允许电子邮件的权限,如果我没有错误的公开个人资料被自动发送。



我还检查了登录页面上的按钮是否有& scope = email。以下是我现在在登录按钮中的页面上的代码:



https://www.facebook.com/v2 0.2 /对话框/ OAuth的CLIENT_ID = XXXXXXXX&安培;状态= XXXXXXXX&安培; RESPONSE_TYPE =代码&安培; SDK = PHP-SDK-5.0.0&安培; REDIRECT_URI = HTTP%3A%2F%2Fxxxxxxx.xxxxxxxxxxxx.co.za%2Fpublic%2Ffacebook%2Flogin -callback& scope = email



我替换了 client_id 状态和域名,因为我不希望公开的信息,因为我不知道人们可以做什么。



让我知道是否需要解决问题。



我使用Facebook指南设置和下载SDK。

解决方案

我解决了这个问题。



你需要从 get 函数。



$ response = $ fb-> get('/ me?fields = id,name,email');



可以找到。



可以查找可以查询的字段的完整列表。



示例

  if(isset($ fb)){
$ helper = $ fb-> getRedirectLoginHelper();
try {
$ accessToken = $ helper-> getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $ e){
$ err_message ='图返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
} catch(Facebook\Exceptions\FacebookSDKException $ e){
$ err_message ='Facebook SDK返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
}

if(strlen($ err_message)< = 0&& isset($ accessToken)){
$ _SESSION ['facebook_access_token'] =(string) $的accessToken;
$ fb-> setDefaultAccessToken($ accessToken);

尝试{
//这里是您指定字段的地方
$ response = $ fb-> get('/ me?fields = id,name,email' );
$ userNode = $ response-> getGraphUser();
/ *处理结果* /
} catch(Facebook\Exceptions\FacebookResponseException $ e){
$ err_message ='图返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
} catch(Facebook\Exceptions\FacebookSDKException $ e){
$ err_message ='Facebook SDK返回错误:'。 $ E->的getMessage();
access_log(facebook_login,IP:$ client_ip \\\\
Facebook错误:$ err_message);
}
}
}


I am new to the whole Facebook API.I am creating a login page for users, and the idea is that they login with Facebook.

At the top of login page I have the following code:

$fb = new Facebook\Facebook([
    'app_id' => "$fb_appid",
    'app_secret' => "$fb_appsecret",
    'default_graph_version' => 'v2.2',
]);

Then a little lower I have the login button:

if(isset($fb)){
    $helper = $fb->getRedirectLoginHelper();
    $permissions = ['email']; // optional
    $fb_login_url = $helper->getLoginUrl("$server_url/public/facebook/login-callback", $permissions);
} else $fb_login_url = "";

if(strlen($fb_login_url) >= 1) echo "<a href='$fb_login_url'><i class='fa fa-2x fa-facebook-square'></i></a>";
else echo "<a href='#' disabled='disabled'><i class='fa fa-2x fa-facebook-square'></i></a>";

The code works up until there, if I click the button, I go through to the Facebook portal, click the allow for the permissions and whatever, then it redirects me to login-callback.

The code on login-callback is:

if(isset($fb)){
    $helper = $fb->getRedirectLoginHelper();
    try {
        $accessToken = $helper->getAccessToken();
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        $err_message = 'Graph returned an error: ' . $e->getMessage();
        access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        $err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
        access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
    }

    if (strlen($err_message) <= 0 && isset($accessToken)) {
        $_SESSION['facebook_access_token'] = (string) $accessToken;
        $fb->setDefaultAccessToken("$accessToken");

        try {
            $response = $fb->get('/me');
            $userNode = $response->getGraphUser();

//            $username = $userNode->getName();
//            $firstname = $userNode->getFirstName();
//            $lastname = $userNode->getLastName();
            print_r($userNode);
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            $err_message =  'Graph returned an error: ' . $e->getMessage();
            access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            $err_message =  'Facebook SDK returned an error: ' . $e->getMessage();
            access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
        }
    }
}

The $userNode looks like this:

Facebook\GraphNodes\GraphUser Object
(
    [items:protected] => Array
        (
            [name] => Jacques Koekemoer
            [id] => xxxxxxxxxxxxxxxxxx
        )

)

I have set the permissions to allow for the email, and if I am not mistaken the public profile is sent automatically.

I have also checked that the button on the login page does have "&scope=email". Below is the code that I have right on the page right now in the login button:

https://www.facebook.com/v2.2/dialog/oauth?client_id=xxxxxxxx&state=xxxxxxxx&response_type=code&sdk=php-sdk-5.0.0&redirect_uri=http%3A%2F%2Fxxxxxxx.xxxxxxxxxxxx.co.za%2Fpublic%2Ffacebook%2Flogin-callback&scope=email

I replaced the client_id, state and domain name because I don't want that information available publicly as I don't know what people can do with it.

Let me know if it is needed to solve the problem.

I used the Facebook guide here to setup and download the SDK.

解决方案

I solved the problem.

You need to request specific fields from Facebook in the get function.

$response = $fb->get('/me?fields=id,name,email');

This can be found here.

A full list of fields that you can query can be found here.

Example

if (isset($fb)) {
    $helper = $fb->getRedirectLoginHelper();
    try {
        $accessToken = $helper->getAccessToken();
    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        $err_message = 'Graph returned an error: ' . $e->getMessage();
        access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        $err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
        access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
    }

    if (strlen($err_message) <= 0 && isset($accessToken)) {
        $_SESSION['facebook_access_token'] = (string)$accessToken;
        $fb->setDefaultAccessToken("$accessToken");

        try {
           // this here is where you specify the fields
            $response = $fb->get('/me?fields=id,name,email');
            $userNode = $response->getGraphUser();
            /* handle the result */
        } catch (Facebook\Exceptions\FacebookResponseException $e) {
            $err_message = 'Graph returned an error: ' . $e->getMessage();
            access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
            $err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
            access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
        }
    }
}

这篇关于Facebook用户图表不返回电子邮件PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:18