I'm obviously doing something wrong when trying to access information from the facebook graph API, here is the code of my page:
It fetches everything I need other than the users email address, I understand you need extended permissions which I have requested as seen in my code below.
require 'src/facebook.php';
$app_id = "211665122244023";
$canvas_page = "http://apps.facebook.com/midcitymafia/";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,publish_actions";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$user_id = $data["user_id"];
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
$graph1 = file_get_contents ("https://graph.facebook.com/".$user_id . "/?accesstoken=" . $data["oauth_token"]);
$graph=json_decode($graph1,true);
}
$userid = $user_id;
$username = $graph['name'];
$usergender = $graph['gender'];
$useremail = $graph['email'];
?>
<br>
<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>
I dumped the $graph variable array to see what information it held, this was the result:
array(7) { ["id"]=> string(10) "1469088864" ["name"]=> string(10) "Jack Brown" ["first_name"]=> string(4) "Jack" ["last_name"]=> string(5) "Brown" ["username"]=> string(11) "thebestjack" ["gender"]=> string(4) "male" ["locale"]=> string(5) "en_GB" }
It doesn't contain the email address so there is obviously a problem with the access token / using the access token to access the graph.
When I go to my authorised apps on my Facebook account settings under this app it says "this apps needs your email address "[email protected]"
you have a $_REQUEST["signed_request"]
for any request, doesn't matter if user authorized your app or not. you have it even for users who are not logged in to Facebook at all.
use php-sdk to retrieve user's profile http://developers.facebook.com/docs/reference/php/facebook-api/
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => '211665122244023',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$login_url = $facebook->getLoginUrl(array(
scope => 'email,publish_actions',
redirect_uri => $canvas_page,));
if($user_id) {
try {
$user_profile = $facebook->api('/' . $user_id,'GET');
$userid = $user_id;
$username = $user_profile['name'];
$usergender = $user_profile['gender'];
$useremail = $user_profile['email'];
} catch(FacebookApiException $e) {
echo("<script> top.location.href='" . $login_url . "'</script>");
error_log($e->getType());
error_log($e->getMessage());
}
} else {
echo("<script> top.location.href='" . $login_url . "'</script>");
}
?>
<?php if (isset($useremail)) : ?>
<br>
<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>
<?php endif; ?>