Question

I got given a code from a friend that displayed the app users birth date on the app canvas.

I have tried to change it in order to echo the users own ID. It works for myself (the creator) and it also works for a friend that i have designated as a developer. But for anyone else it brings up this error:

Warning: file_get_contents(https://graph.facebook.com/me?
fields=id&access_token=228073413942115|460d2e1ffd0b4cc6b5cb480d53f60d6e)
[function.file-get-contents]: failed to open stream: HTTP request failed! 
HTTP/1.0 400 Bad Request in /home/robsdmr/public_html/fb/index.php on line 34

The code is as follows.

<?php
require_once("src/facebook.php");

$app_id = "xxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$canvas_page = "https://apps.facebook.com/roblewtest/" .$app_id;


$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));

$access_token = $facebook->getAccessToken();


$auth_url = "https://www.facebook.com/dialog/oauth?scope=user_id&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 {

}

$graph_url = "https://graph.facebook.com/me?fields=id&access_token=" . $access_token;
$result = json_decode(file_get_contents($graph_url));

echo "{$result->id}"

?>

I am pretty new to this so pointers would be great

I have a feeling that it is the scopes that i am not quite to grips with, but i really need this working.

Thanks a lot guys n gals.

R.

Was it helpful?

Solution

Try using SDK's graph methtod:

$result = $facebook->api('/me?fields=id');

And also to get login url you could use:

$auth_url = $facebook->getLoginUrl();

Documentation:api(), getLoginUrl()

Also simplified you code, see here.

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