How get facebook user ID from my application?

后端 未结 4 1502
傲寒
傲寒 2020-12-21 18:20

I created a facebook application and send request to a user through below code.Now when a user allow my application then i need his id and email address into my canvas url.B

相关标签:
4条回答
  • 2020-12-21 18:26

    I am not familiar with php to comment on the code above. However in general it would be better to use javascript sdk for login alone (faced the same issue in asp.net). That way post user login, the control comes back to the same page where it was initiated. For example:

        <div id="fb-root"></div>
        <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
        <script type="text/javascript">
            FB.init({
                appId: <app_id>, cookie: true, status: true, xfbml: true
            });
    
            function fblogin() {
                FB.login(function (response) {
                    //Control comes back here
                    if (response.session) {
                        alert(response.session.access_token);
                        var url = '/me?fields=name,email';
                        FB.api(url, function (response) {
                            //You are now in the same page with the data you wanted. If necessary redirect.
                            alert(response.name);
                            alert(response.email);
                        });
                    }
                    else {
                        alert("User did not login successfully");
                    }
                }, { perms: 'email' });
            }
        </script>
        <a href="#" onclick="fblogin(); return false;"><img src="../Images/social_facebook.png" /></a>
    
    0 讨论(0)
  • 2020-12-21 18:32

    First of all, your code is "badly" written. The catch block in your code "when executed" does not mean that the user is not logged in. It means that something went wrong in your try block (a.k.a bad Facebook Call).

    Now for your question, you just need to place this in your try block:

    $data     =   $facebook->api('/me?fields=id,email');
    

    IMPORTANT NOTE:
    You may not get the user real email, the user may choose to not share it with you and this way Facebook will create a "mirror" email to share with your App.

    0 讨论(0)
  • 2020-12-21 18:47

    At last i solve my problem and now i can easily get the facebook user id and email address and other related information.Using this code i can hit to facebook for particular user's authentication and when user accept my application then i can get his/her facebook user id, email and other information into $user_info array.Hope it will help you.

    Just see my code below:

    <?php
    require_once 'facebook.php';
    $facebook = new Facebook(array(
      'appId'  => '########',
      'secret' => '##########################',
      'cookie' => true,
    ));
    
    $req_perms = "publish_stream,offline_access,user_status,email,read_stream"; 
    $session = $facebook->getSession();
    $loginUrl = $facebook->getLoginUrl(array('canvas'=> 1,'fbconnect' => 0,'req_perms' => $req_perms));
    $user_info = null;
    if (!$session) 
        { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";exit;}
    else{
         try {  $user_info = $facebook->api('/me');  } 
         catch (FacebookApiException $e) 
             { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; exit;}
        }
    ?>
    <!doctype html>
    <html>
    <head>
    </head>
    <body>
    <?php 
    $user_name=$user_info['name'];
    echo "Hi $user_name ! Your Facebook ID is: ".$user_info['id']."<br/>"; 
    echo "Hi $user_name ! Your E-mail Address is: ".$user_info['email']."<br/>"; 
    echo "Thanks for accepting our application.";
    //print_r($user_info);
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-21 18:48

    The Facebook ID & email for the current user can be accessed through:

    $me['id'];
    $me['email'];
    

    Is that what you're after?

    0 讨论(0)
提交回复
热议问题