在Facebook php SDK中打开Auth Dialog

时间:2012-03-24 13:23:34

标签: php facebook sdk oauth

我整合Facebook连接在我的网站使用php SDK ..一切正常,除了我想要而不是重定向到Facebook进行身份验证,我想打开OAuth对话..目前我使用以下网址进行身份验证,。它重定向到Facebook但没有打开OAuth Dailog ..

https://www.facebook.com/dialog/oauth?client_id=XXXXXXXXXXX&redirect_uri=http%3A%2F%2Fwww.setsail2nz.com.au%2Fmicrosite%2Ffbalbums.php%3Fpid%3D1&state=ad89eddd7f71e7337785f604710c97e8&scope=user_photos%2Cpublish_stream%2Cmanage_friendlists%2Cemail&display=popup

有什么想法吗?

编辑: 我知道如何使用Facebook JS SDK。但有没有办法用PHP做到这一点?

编辑2:好的,现在我正在使用JS SDK,但仍然没有打开对话框..这是我的代码

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
     appId      : 'XXXXXXXXXX', // App ID
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     xfbml      : true,  // parse XFBML
     oauth   : true // enables OAuth 2.0
    });
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

  function fblogin()
  {
    FB.login(function(response)
    {
        alert(response);

    },{perms: "user_photos,publish_stream,manage_friendlists,email"});
  }
</script>

2 个答案:

答案 0 :(得分:1)

使用Javascript SDK https://developers.facebook.com/docs/reference/javascript/

和FB.login,传递您所需的权限

答案 1 :(得分:0)

回答有关使用PHP SDK检测对话框响应的问题:

问题是getloginURL只接受一个成功或错误时使用的URL参数。我需要检测用户是否在权限对话框中单击了“确定”或“取消”,因此我可以重定向到预权限对话框页面,或显示正确的登录内容。

我所做的就是检查url参数中是否存在'error_reason',如果错误是'user_denied',则采取适当的措施。可能有更好的方法,但这对我有用。

<?php
    //Facebook Authentication
    $user = $facebook->getUser();

    //Get Login URL
    $loginUrl = $facebook->getLoginUrl(); 

    //User is logged in
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }

    //User is not logged in
    if (!$user) {
        //Check if error_reason was generated and if the user denied login
        if (isset($_REQUEST['error_reason']) && ($_REQUEST['error_reason']=='user_denied')) {
            //user cancelled permissions dialog thus not logged in to app, redirect elsewhere
            echo "<script type='text/javascript'>top.location.href = 'YOUR REDIRECT URL';</script>";
            exit;
        } else {
            //user not logged in so initiate permissions dialog
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
    }

    //get user basic description
    $userInfo = $facebook->api("/$user");
?>