Stu Rohrer
Stu Rohrer

Reputation: 1

FB doesn't return access_token for my app, no error msg specified.

This is my first FB app, website integraton, and I can't get FB's sample php code to return an access_token.

I have checked: - app setup screens on developer.facebook.com seem ok (no warnings or errors) - include latest github version of facebook.php on page - app_id and app_secret are correct

The code below is copied from the FB docs, but $response doesn't return anything and $user->name is empty.

Can anyone give me a hand here? Is there a debug technique that could tell me why it's failing to succeed in getting a access_token?

$token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = @file_get_contents($token_url);

 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 echo("Hello " . $user->name)."<br>";

Upvotes: 0

Views: 671

Answers (1)

Danish Iqbal
Danish Iqbal

Reputation: 1464

Instead of above code try the below

<?php 

require_once 'library/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'app id',
  'secret' => 'secret',
  'cookie' => true,
)); 

     $app_id = '149865361795547';

     $canvas_page = "canvas page link ";

     $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=email,read_stream&response_type=token");

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

    //get the user access token
            $atoken = $facebook->getAccessToken();
            echo "</br>" . 'User Access_Token:' . $atoken;

     //get the user id 
            $UserId = $data["user_id"];
            echo 'UserId;' . $UserId;
}
?>

Upvotes: 1

Related Questions