FACEBOOK SDK v4 返回空白电子邮件地址

2022-01-08 00:00:00 sdk php facebook

我正在使用 facebook sdk v4,但我似乎无法获得电子邮件地址.这是我的代码.我已经搜索了互联网上的每一个链接并尝试了每一件事.还尝试了不同的脚本,但我仍然收到空电子邮件.这是我的代码.

I'm using facebook sdk v4 and i can't seem to get the EMAIL ADDRESS. Here is my code. I have searched each and every link on internet and tried every thing. Also tried different scripts but still i'm getting null email. Here is my code.

/* INCLUSION OF LIBRARY FILEs*/
    require_once( 'lib/Facebook/FacebookSession.php');
    require_once( 'lib/Facebook/FacebookRequest.php' );
    require_once( 'lib/Facebook/FacebookResponse.php' );
    require_once( 'lib/Facebook/FacebookSDKException.php' );
    require_once( 'lib/Facebook/FacebookRequestException.php' );
    require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
    require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
    require_once( 'lib/Facebook/GraphObject.php' );
    require_once( 'lib/Facebook/GraphUser.php' );
    require_once( 'lib/Facebook/GraphSessionInfo.php' );
    require_once( 'lib/Facebook/Entities/AccessToken.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

    use FacebookFacebookSession;
    use FacebookFacebookRedirectLoginHelper;
    use FacebookFacebookRequest;
    use FacebookFacebookResponse;
    use FacebookFacebookSDKException;
    use FacebookFacebookRequestException;
    use FacebookFacebookAuthorizationException;
    use FacebookGraphObject;
    use FacebookGraphUser;
    use FacebookGraphSessionInfo;
    use FacebookFacebookHttpable;
    use FacebookFacebookCurlHttpClient;
    use FacebookFacebookCurl;

/*PROCESS*/

    //1.Stat Session
     session_start();

    //check if users wants to logout
     if($_REQUEST['logout']==1){
        unset($_SESSION['fb_token']);
     }

    //2.Use app id,secret and redirect url 
    $app_id = 'xxxxxxxxxx';
    $app_secret = 'xxxxxxxxxxxxxxx';
    $redirect_url='http://someurl.com/';

    //3.Initialize application, create helper object and get fb sess
     FacebookSession::setDefaultApplication($app_id,$app_secret);
     $helper = new FacebookRedirectLoginHelper($redirect_url);
     $sess = $helper->getSessionFromRedirect();

    //check if facebook session exists
    if(isset($_SESSION['fb_token'])){
        $sess = new FacebookSession($_SESSION['fb_token']);
    }

    //logout
    $logout = 'http://dryvr.co/fb/?logout=1';

    //4. if fb sess exists echo name 
        if(isset($sess)){
            //store the token in the php session
            $_SESSION['fb_token']=$sess->getToken();
            //create request object,execute and capture response
            $request = new FacebookRequest($sess,'GET','/me');
            // from response get graph object
            $response = $request->execute();
            $graph = $response->getGraphObject(GraphUser::classname());
            // use graph object methods to get user details
            $name = $graph->getName();
            $id = $graph->getId();
            $image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
            $email = $graph->getProperty('email');
            echo "hi $name <br>";
            echo "your email is $email <br><Br>";
            echo "<img src='$image' /><br><br>";
            echo "<a href='".$logout."'><button>Logout</button></a>";
        }else{
            //else echo login
            echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with facebook</a>';
        }

我使用了 $email = $graph->getProperty('email'); 但我仍然无法访问电子邮件地址并且它返回空白电子邮件.我该怎么办?想法?

I have used $email = $graph->getProperty('email'); but still I can not access the email address and it is returning blank email. What should I do? Thoughts?

推荐答案

在这里看看我的回答:

  • 如何从 Facebook API 版本 v2.4 获取用户电子邮件和生日

您需要指定要从 v2.4 开始的 Graph API 返回的每个字段,例如

You need to specify each field you want returned from the Graph API starting with v2.4, e.g.

$request = new FacebookRequest($sess,'GET','/me?fields=id,name,email');

这一切都在 Facebook 文档中.

It's all in the Facebook docs.

相关文章