我在尝试从facebook图形API访问信息时显然做错了,这是我的页面代码:
除了用户的电子邮件地址之外,它还提取了我需要的所有内容,我知道您需要扩展的权限,我在下面的代码中看到了这些权限。
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; ?>
我转储$ graph变量数组以查看它保存的信息,结果如下:
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" }
它不包含电子邮件地址,因此访问令牌/使用访问令牌访问图表时显然存在问题。
当我在此应用程序下的Facebook帐户设置上访问我的授权应用程序时,它说“此应用程序需要您的电子邮件地址”blahblah@yahoo.co.uk“
答案 0 :(得分:0)
对于任何请求,您都有$_REQUEST["signed_request"]
,无论用户是否授权您的应用都无关紧要。即使对于完全没有登录Facebook的用户,也可以使用它。
使用php-sdk检索用户的个人资料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; ?>