dqpc1845 2011-05-24 12:16
浏览 114

Facebook登录使用PHP或Javascript SDK无法正常工作

as the title states, I am having some trouble getting the user logged in on my Facebook application.

When the user is logged out, and they click log in, the page reloads and then displays a Facebook logo with the text Go to Facebook.com below it, and when I try using the Javascript SDK, I do get the login page in a popup box, but once I press Login, the popup redirects to a XD Proxy page, where I get stuck, and I have to manually close the popup to have the callback execute.

In both cases I get this error in the Chrome console,

Error I get in console, when using the Javascript SDK:

Unsafe JavaScript attempt to access frame with URL https://www.facebook.com/dialog/permissions.request?etcetc... from frame with URL http://subdomain.mysite.com/folder/?etcetc... Domains, protocols and ports must match.

Error I get in console, when using the PHP SDK:

Unsafe JavaScript attempt to access frame with URL http://apps.facebook.com/my_app/ from frame with URL https://www.facebook.com/login.php?etcetc... Domains, protocols and ports must match.

Here is my code,

PHP:

<?php
require 'libs/facebook/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'APP ID',
  'secret' => 'SECRET',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
  echo "<a href='".$logoutUrl."'>Login</a>";
} else {
  $loginUrl = $facebook->getLoginUrl();
  echo "<a href='".$loginUrl."'>Login</a>";
}
?>

I am using version 3.0 of the PHP SDK.

After the PHP failed, I tried using the Javascript SDK instead, to handle the login and logout,

Javascript:

FB.init({
    appId       : 'APP ID',
    status      : true, // check login status
    cookie      : true, // enable cookies to allow the server to access the session
    xfbml       : true // parse XFBML
}); 

FB.getLoginStatus(function(response) {

    if (response.session) {

        $('#login').hide();
        $('#logout').show();

    } else {

        $('#logout').hide();
        $('#login').show();

    }
});


$("#logout").live('click', function(){
    FB.logout(function(response) {
        window.location.reload();
    }); 
});

$("#login").live('click', function(){
    FB.login(function(response) {
        window.location.reload();
    }); 
});

Any help would be greatly appreciated, thank you!

  • 写回答

1条回答 默认 最新

  • dongzhuanlei0768 2011-05-24 14:56
    关注

    Ok, so after banging my head against the board for a while, I came up with this, and it does the trick!

    Using the PHP SDK, it is redirecting the user to the login page if they are not logged in, and setting the Logout URL with the next parameter for later use wherever/whenever you want it.

    And no more XSS errors! :)

    if ($user) {
        $logoutUrl = $facebook->getLogoutUrl(array('next'=>'http://apps.facebook.com/your_app/'));
    } else {
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://apps.facebook.com/your_app/'));
        echo "<script>top.window.location='".$loginUrl."';</script>";
    }
    

    I hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛