Question

I am successfully able to authorize my canvas app using this code but I am only able to get the user id.I need user name,age friendlist also.Plz help me.

I used Print_r and get this

Array ( [algorithm] => HMAC-SHA256 [expires] => 1366020000 [issued_at] => 1366012878 [oauth_token] => BAAFaFlrFWXYBADDEIui8rTtcEWldiZAGlHZB1mGSZoQ6E9fU9vkweYnx2jvj4SSDNRUd465YcAiz4hI3jlQlXLBmB0jVXZBeoAiSyO5P2TQoIQVt9SQRWVJEuTGtcyQzazZCi20MWhDqBsa1qgYWKloSRWOkKBdPDBZCI0exZA13Jygh73hsr9ldVvFy8ClgEb8jIZBblCArGT3lb7g [user] => Array ( [country] => pk [locale] => en_US [age] => Array ( [min] => 21 ) ) [user_id] => 100000 

PHp code

 $app_id = "    xxxxxxxxx";

$canvas_page = "xxxxxxxx";

$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 {

    print_r($data) ;
    echo ("Welcome User: " . $data["user_id"]);
    echo ("Welcome User: " . $data["age"]); //i tried this,but got undefined message?

}
Was it helpful?

Solution

You need to do an API request or use FQL in order to get more information about the user. http://developers.facebook.com/docs/reference/php/facebook-api/ has examples that show you how to get the user's name with using the API method. And then here is the API documentation for FriendsList, and here is the FQL docs. Make sure you have the read_friendlists permission. Let me know if you have any questions :)

Example with code:
Follow the instructions at http://developers.facebook.com/docs/reference/php/ to install the PHP SDK from https://github.com/facebook/facebook-php-sdk/ .

Then go to http://developers.facebook.com/docs/reference/php/facebook-api/ and copy the first example into a file. Make sure to update the app id, app secret, and that your require_once is set to right location.

Then replace

  try {

    $user_profile = $facebook->api('/me','GET');
    echo "Name: " . $user_profile['name'];

with

try {
        //Get User Name
        $user_profile = $facebook->api('/me','GET');
        //Get User's Friends
        $friends_list = $facebook->api('/me/friends', 'GET');
        echo "<h1>Name: " . $user_profile['name']."</h1>";
        echo "<h2>Friends:</h2>"; 
        foreach ($friends_list['data'] as $friend)
        {
            echo $friend['name']."<br>";
        }

OTHER TIPS

You can access the user endpoint in the Graph API sending a signed request, for example querying https://graph.facebook.com/me?fields=id,name will give you the user name of the authenticated user. See http://developers.facebook.com/docs/reference/api/user/ for reference.

I suggest you use the Facebook PHP SDK

you can get it from github and it makes things easier. I believe it has a basic example that does just what you want.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top