这是我的第一个FB应用程序,网站集成,我无法获取FB的示例php代码来返回access_token。
我检查过: - developer.facebook.com上的应用设置屏幕似乎没问题(没有警告或错误) - 包括页面上最新的facebook.php的github版本 - app_id和app_secret是正确的
以下代码是从FB文档中复制的,但$response
并未返回任何内容,$user->name
为空。
有人能帮我一把吗?是否有一种调试技术可以告诉我为什么它没有成功获得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>";
答案 0 :(得分:1)
代替上述代码,请尝试以下
<?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;
}
?>