Facebook Iframe 应用程序身份验证?

发布于 2024-10-11 15:36:57 字数 1117 浏览 2 评论 0原文

我开发了一个 Facebook 应用程序,该应用程序在 Facebook 画布的 iframe 内运行。为了使其正常工作,我向用户请求扩展权限。如果用户尚未授权该应用程序,我将使用 PHP SDK 中的 getLoginUrl() 方法将他/她发送到登录页面。

它有效,但并不漂亮。该方法将用户发送到身份验证页面之前的登陆页面。它看起来像这样:

Facebook Landing Page

当我单击“转到 Facebook.com”时,我会看到权限请求的实际页面(如果我打印网址,复制它并将其输入到新的浏览器窗口中,我也可以直接进入权限页面)。当我从 Iframe 进行重定向时,如何让 Facebook 跳过此步骤?

我的代码如下所示(使用 CodeIgniter 和 Facebook PHP SDK):

$this->facebook = new Facebook(array(
  'appId'  => '{MY_APP_ID}',
  'secret' => '{MY_SECRET}',
  'cookie' => TRUE,
  'domain' => $_SERVER['SERVER_NAME']
));

$this->facebook->getSession();

try {
  $this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
  $this->me = NULL;
}

if ( is_null($this->me) ) {
  redirect($this->facebook->getLoginUrl(array(
    'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
    'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
  )));
}

I have developed a Facebook application that runs inside an iframe in the Facebook canvas. For it to work properly I request extended permissions from the user. If the user hasn't authorized the application I send him/her to a login page with the getLoginUrl() method in the PHP SDK.

It works, but it's not pretty. The method sends the user to a landing page before the authentication page. It looks like this:

Facebook Landing Page

When I click "Go to Facebook.com" I see the actual page for permission requests (I also get right to the permissions page if I print the url, copy it and enter it into a new browser window). How do I make Facebook skip this step when I do the redirect from an Iframe?

My code looks like this (using CodeIgniter and Facebook PHP SDK):

$this->facebook = new Facebook(array(
  'appId'  => '{MY_APP_ID}',
  'secret' => '{MY_SECRET}',
  'cookie' => TRUE,
  'domain' => $_SERVER['SERVER_NAME']
));

$this->facebook->getSession();

try {
  $this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
  $this->me = NULL;
}

if ( is_null($this->me) ) {
  redirect($this->facebook->getLoginUrl(array(
    'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
    'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
  )));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2024-10-18 15:36:57

我认为您需要重定向父框架(即 _top)而不是 iFrame 本身?

I think you need to redirect the parent frame (i.e. _top) rather than the iFrame itself?

待"谢繁草 2024-10-18 15:36:57

我这样做的方法是使用以下内容设置 INDEX.PHP 文件

 //如果用户已登录且会话有效。
    如果($fbme){
        //使用遗留方法调用和传递的fql查询示例

参数
尝试{
$fql = "选择姓名、家乡位置、性别、
pic_square 来自用户,其中 uid=" 。
$uid;
$参数=数组(
'方法' => 'fql.query',
'查询' => $fql,
'回调' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
捕获(异常$o){
d($o);
}
}

将你的画布网址指向 http://yoursite.com/INDEX.php

中的回调网址INDEX.PHP 中的上述代码设置了授予权限后在何处查看。

FBMain.php 看起来像这样

//在这里设置应用程序url
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']

=“http://www.tyoursite.com/YOURMAINPAGE.php/”;

$fbconfig['http://apps.facebook.com/CANVASBASEURL']

=“http://apps.facebook.com/CANVASBASEURL”;

$uid = null; //脸书用户ID

尝试{
    include_once“facebook.php”;
}
捕获(异常$o){
    回显“<前>”;
    打印_r($o);
    回显“

”;
}
// 创建我们的应用程序实例。
$facebook = 新 Facebook(数组(
'应用程序ID' => $fbconfig['APPID'],
'秘密' => $fbconfig['秘密'],
'cookie'=>真的,
));

//Facebook认证部分
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
大批(
'画布' => 1、
'fbconnect' =>; 0,
'req_perms'=>'电子邮件、publish_stream、status_update、用户生日、用户位置'

);

$fbme = null;

如果(!$会话){
echo "<脚本类型='text/javascript'>top.location.href

= '$loginUrl';";
出口;
}
别的 {
尝试 {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');

 } catch (FacebookApiException $e) {
        echo "<脚本类型='text/javascript'>top.location.href

= '$loginUrl';";
出口;
}
}

函数 d($d){
    回显“<前>”;
    打印_r($d);
    回显“

”;
} ?>

它更清楚一点。我花了一段时间才弄清楚,但我到达那里,认为我能提供帮助。

The way I do it is set up an INDEX.PHP file with the following

    //if user is logged in and session is valid.
    if ($fbme){
        //fql query example using legacy method call and passing

parameter
try{
$fql = "select name, hometown_location, sex,
pic_square from user where uid=" .
$uid;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}

Then point your canvas url to http://yoursite.com/INDEX.php

The callback url in the above code which will be in INDEX.PHP sets where to look after permissions are granted.

FBMain.php looks like this

//set application urls here
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']

= "http://www.tyoursite.com/YOURMAINPAGE.php/";

$fbconfig['http://apps.facebook.com/CANVASBASEURL']

= "http://apps.facebook.com/CANVASBASEURL";

$uid            =   null; //facebook user id

try{
    include_once "facebook.php";
}
catch(Exception $o){
    echo '<pre>';
    print_r($o);
    echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => $fbconfig['APPID'],
  'secret' => $fbconfig['SECRET'],
  'cookie' => true,
));

//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
        array(
        'canvas'    => 1,
        'fbconnect' => 0,
        'req_perms'=>'email,publish_stream,status_update,user_birthday,user_location'
        )
);

$fbme = null;

if (!$session) {
    echo "<script type='text/javascript'>top.location.href

= '$loginUrl';";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');

    } catch (FacebookApiException $e) {
        echo "<script type='text/javascript'>top.location.href

= '$loginUrl';";
exit;
}
}

function d($d){
    echo '<pre>';
    print_r($d);
    echo '</pre>';
} ?>

Hope its a little clearer. It took me a while to figure it out, but I got there, thought I would help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文