当用户使用Facebook应用中心的"访问网站"按钮时,无法获得access_token

Mar*_*chi 4 oauth facebook-apps

我正在尝试使用应用中心宣传我的应用,但是从网络预览我无法访问该网站.调用的链接是http://www.myappsite.it/?fb_source=appcenter&fb_appcenter=1&code=a_long_string

从myappsite的index.php我使用这个和平的PHP代码让用户来自Facebook

$code = $_REQUEST["code"];

if($_REQUEST['state'] == $_SESSION['state'] && !$user && strlen($code) > 0) 
{
$token_url = "https://graph.facebook.com/oauth/access_token?" . 
"client_id=" . $appId . "&redirect_uri=" . urlencode("http://www.myappsite.it/") . 
"&client_secret=" . $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);
}
Run Code Online (Sandbox Code Playgroud)

但$ params ['access_token']为空,因为$ token_url返回

{
"error": {
    "message": "Error validating verification code.",
    "type": "OAuthException",
    "code": 100
    }
}
Run Code Online (Sandbox Code Playgroud)

如何让用户从应用程序中心预览网页登录?

Igy*_*Igy 6

来自文档的Authenticated Referrals部分(App中心计为经过身份验证的推荐)

请参阅使用"查询字符串"设置时的特殊注意事项

如果您想使用服务器端身份验证流程,请务必确保在交换访问令牌代码时正确传递redirect_uri参数.您应该将redirect_uri参数设置为站点的点击后URL而不使用code参数.在大多数情况下,URL看起来像:

http://www.example.com/?fb_source=search&code=CODE_HERE剥离代码参数后,它将变为:

http://www.example.com/?fb_source=search这是应该在redirect_uri中设置的值.请确保此逻辑是动态的,因为附加到点击后到达网址的查询参数可能会发生变化.

使用上面的示例,这意味着您的redirect_uri参数应该是http://www.myappsite.it/?fb_source=appcenter&fb_appcenter=1