Asare Worae
Asare Worae

Reputation: 43

retrieve user id inside facebook page tab

I'm trying to build an app that lets users vote in 4 different categories, I want to save the User id so that I can make sure someone can only vote once.

I know you can get the user id when a user interacts with the tab, I've read something about fb_sig_profile_user but I don't know how to use this.

I've read a little about this on the facebook dev page but it isn't very clear.

If I understand correctly you can get the user id after a user clicks on something and the facebook tab does an ajax call. Then at that moment you can grab the user id but I don't know how.

Upvotes: 1

Views: 1153

Answers (3)

Sumit Chikane
Sumit Chikane

Reputation: 139

//use this to get facebook user id

<?php session_start();?>    
    <?php require 'src/facebook.php';
        $app_id = "your_app_id";
        $app_secret = "your_secret_key";
        $facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret

        ));

             $canvas_page = "https://www.facebook.com/page_name/app_appid"; //this is you facebook app url

             $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
                    . $app_id . "&redirect_uri=" . urlencode($canvas_page);

             $signed_request = $_REQUEST["signed_request"];

             list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

             $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

             if (empty($data["user_id"])) {
                    echo("<script> top.location.href='" . $auth_url . "'</script>");
             } else {
                    echo $data["user_id"];
                    $_SESSION['fbuser']=$data["user_id"];
             } 
?>

Upvotes: 0

Julio Santos
Julio Santos

Reputation: 3895

Facebook will make a POST request to your app with a signed_request parameter which, after you decode as described in the docs, will yield a hash of information such as the user's country.

If the user has authorized your app, then that same parameter will contain additional information such as the user's Facebook id.

Upvotes: 0

DMCS
DMCS

Reputation: 31860

I have many Facebook page tabs that require the Facebook user id to ensure no duplicate entries are made for one user. What you have to do is step up a Facebook app. Configure it as a Page Tab app and put it on your Facebook page. Once it is there, then you do a Facebook login popup asking for user permissions. After that, you can use the SDK to query the current user to get their ID (or you can decode the signed_request form post parameter that facebook posts to your tab app). You can also do similar things in non-page tab apps wether it is a standalone website or a iFramed canvas app.

Upvotes: 1

Related Questions