[英]Facebook authorization (oauth) redirecting app out of iFrame (in php)
[英]Facebook Authorization for an iFrame App in PHP
我需要获取用户点击Facebook iFrame应用程序的用户ID。 在筛选所有虚假信息和过时信息时,我想我已经看到了正确的信息。
当用户点击页面时,我需要让他们授权应用程序。 在我的PHP中我这样做:
$facebook = new Facebook($appData);
$user = $facebook->getUser();
if(!$user) {
echo '<script>top.location.href="'.$facebook->getLoginUrl().'";</script>';
die();
}
这给了我授权请求,但然后将我推回到我的服务器,而不是iFrame App的URL(http://app.facebook.com/blah)。 我手动构建了URL并尝试将URL设置为app.facebook.com URI,但之后我收到一条错误消息,指出该URL对应用程序无效。
如何在授权后将其重定向回应用程序? 这个应用程序将住在Facebook内,一般不在外面访问,所以我不是在寻找Facebook Connect登录。
编辑
这是我在使用request_uri时得到的确切错误:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
您是否尝试过设置redirect_uri
参数。
if(!$user) {
$params = array('redirect_uri' => 'http://app.facebook.com/blah/');
die('<script>top.location.href="'.$facebook->getLoginUrl($params).'";</script>');
}
去:
http://developers.facebook.com/apps
在您的开发人员应用程序设置中,您需要更改URL设置,以便Facebook知道您的应用程序是一个画布应用程序。 在设置中,请务必删除Web选项卡中的所有URL,然后确保在Facebook选项卡上正确设置所有URL。
您可以使用图形API跟踪示例代码以在iframe应用程序中授权facebook应用程序
<?php
ob_start();
session_start();
/*
* App Config
*/
$config= array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'canvas'=>"http://apps.facebook.com/sampleapp/");
$GRAPH_URL = "https://graph.facebook.com/";
$scope = "publish_stream,email";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id=".$config['appId']."&redirect_uri=".urlencode($config['canvas']). "&scope=" . $scope;
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if(is_array($data)){
$authToken = $data['oauth_token'];
$userId = $data['user_id'];
$_SESSION['auth_token'] = $authToken;
//check for permission//
$permissions = json_decode(curl_get_file_contents($GRAPH_URL . "me/permissions?access_token=" . $authToken), TRUE);
if(array_key_exists('publish_stream', $permissions['data'][0]) ) {
$post = array('client_id'=>$config['appId'],'redirect_uri'=>$config['canvas'].'','client_secret'=>$config['secret'],'type'=>'client_cred');
$token_url="https://graph.facebook.com/oauth/access_token";
$response = curlpost($token_url,$post);
$params = explode('&',$response);
if(isset($params[0])){
$token = explode('=',$params[0]);
if($token[0]=='access_token'){
$access_token = $token[1];
$_SESSION['access_token']=$access_token;
$_SESSION['authorized']=1;
echo("<script> top.location.href='" . $config['canvas']."home.php'; </script>");
}
}else{
echo("<script> top.location.href='" . $config['canvas']."error.php'; </script>");
}
}else{
$url='https://graph.facebook.com/oauth/authorize?client_id='.$config['appId'].'&redirect_uri='.urlencode($config['canvas']).'&display=page&scope=publish_stream,email&type=user_agent';
echo("<script> top.location.href='" . $url. "'</script>");
}
}else{
echo("<script> top.location.href='" . $config['canvas']."error.php';</script>");
}
?>
请查看此链接了解详情
http://forum.bharathlisting.com/showthread.php?tid=13&pid=20#pid20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.