开发者

Facebook Authorization for an iFrame App in PHP

开发者 https://www.devze.com 2023-03-24 19:47 出处:网络
I need to get the User ID of a user hitting a Facebook iFrame application. While sifting through all of the bogus and outdated information, I think I\'ve come across the correct info.

I need to get the User ID of a user hitting a Facebook iFrame application. While sifting through all of the bogus and outdated information, I think I've come across the correct info.

When the user hits the page, I need to have them authorize the app. In my PHP I do this:

$facebook = new Facebook($appData);
$user = $facebook->getUser();

if(!$user) {
    echo '<script>top.location.href="'.$facebook->getLoginUrl().'";</script>';
    die();
}

This gives me the authorization request, but then pushes me back to my server, not the iFrame App's URL (http://app.facebook.com/blah). I manually constructed the URL and tried to set the URL to the app.facebook.com URI but then it I get an error saying the URL is not valid for the app.

How do you get it to redirect back to the app after authorization? This app will live inside Facebook, not be generally accessible outside, so开发者_如何学JAVA I'm not looking for Facebook Connect login.

Edit

This is the exact error I get when I fiddle with the 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.


Have you tried setting the redirect_uri parameter.

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


Go to:

http://developers.facebook.com/apps

In the your developer application settings on you need to change the URL settings so that Facebook knows your app is a canvas app. In the settings, be sure to delete all the URLs that are in the Web tab and then make sure that all the URLs are appropriately set in the On Facebook tab.


You can following sample code to authorize facebook application in an iframe app using graph api

<?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>");        
}


?>

Check this link for details

http://forum.bharathlisting.com/showthread.php?tid=13&pid=20#pid20

0

精彩评论

暂无评论...
验证码 换一张
取 消