0

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?

}
user1452376
  • 129
  • 2
  • 12

2 Answers2

0

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

smarques
  • 708
  • 7
  • 22
0

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>";
        }
asifrc
  • 5,781
  • 2
  • 18
  • 22
  • what specifically do you need illustrated by code? (any example code I would've used should be in the first link) – asifrc Apr 15 '13 at 12:13
  • I want to echo the email and friendslist of the user,I tried scraching my head using above refrences,but of no vail.i ll be musch gratefull to u for the answer. – user1452376 Apr 15 '13 at 12:19
  • I answered your email question [here](http://stackoverflow.com/questions/16014307/how-to-get-e-mail-when-logging-into-canvas-application-using-fb-graph/16016283#16016283). I answered there so that someone looking for the answer can find it more easily. Once again let me know if this was helpful. – asifrc Apr 15 '13 at 13:24