Facebook授权使用PHP构建iFrame应用程序

时间:2011-08-03 17:38:31

标签: php facebook

我需要获取用户点击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.

3 个答案:

答案 0 :(得分:5)

您是否尝试过设置redirect_uri参数。

if(!$user) {
  $params = array('redirect_uri' => 'http://app.facebook.com/blah/');
  die('<script>top.location.href="'.$facebook->getLoginUrl($params).'";</script>');
}

答案 1 :(得分:0)

转到:

http://developers.facebook.com/apps

在您的开发人员应用程序设置中,您需要更改URL设置,以便Facebook知道您的应用程序是一个画布应用程序。在设置中,请务必删除网络标签中的所有网址,然后确保在在Facebook 标签上正确设置了所有网址。

答案 2 :(得分:0)

您可以使用图表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