Facebook应用程序重定向

时间:2013-03-17 20:51:46

标签: php facebook

我在index.php中有以下代码:

 $config = array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'cookie' => true
);
$facebook = new Facebook($config);

$app_name = "AppName";
$fbPermissions = 'email';  
$validUser = 1;
$fbuser = $facebook->getUser(); // get user
$fbname = "";
$fbmail = "";
$fbid = 0;
$user_profile = null;

if(!$fbuser) 
{
$validUser = 0;
}
else
{
 try {
    $user_profile = $facebook->api('/me'); //user profile
    $user_permissions = $facebook->api("/me/permissions"); //list of user permissions
  } catch (FacebookApiException $e) {
     $fbuser = null;
     $user_permissions = null;
  }

  $permissions_needed = explode(',',$fbPermissions); // permission required to proceed
  foreach($permissions_needed as $per) //loop thrugh each permission
  {
    if ($user_permissions != null) { 
        if(!array_key_exists($per, $user_permissions['data'][0])){
            $validUser = 0;
        }
    }
  }
}
if($validUser == 1){
$fbname = $user_profile["last_name"]." ".$user_profile["first_name"];
$fbmail = $user_profile["email"];
$fbid = $user_profile["id"];
}

if(isset($_POST['submit'])) {
header( 'Location: writetocsv.php?id='.$_POST['fbid'].'&name='.$_POST['fbname'].'&email='.$_POST['email'] ) ;
}

    if($signed_request = parsePageSignedRequest()){
        if($signed_request->page->liked) {
            if(!userIsInDatabase($fbid)){
                include("pages/fanpage.php");
                echo "fanpage";
            }
            else{
                include("pages/thankyou.php");
                echo "thankyoupage";
            }
        } else {
            include("pages/notfanpage.php");
            echo "notafanpage";
        }
    }

?>

 <script>
      // Additional JS functions here
      function login() {
        FB.login(function(response) {
            if (response.authResponse) {
                // connected        

            } else {
                // cancelled
            }
        },{scope:"email"});
        }
      window.fbAsyncInit = function() {
        FB.init({
          appId      : <?php echo AppInfo::appID(); ?> , // App ID
          channelUrl : 'channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });

        // Additional init code here
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // connected
            } else if (response.status === 'not_authorized') {
                // not_authorized

                login();
            } else {
                // not_logged_in

                login();
            }
        });
      };

      // 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));
      </script>

好的,让我解释一下。当新用户连接到我的标签页应用页面时,会向他显示notafanpage.php。在他喜欢我的页面之后,标签会自动刷新并向他显示fanpage.php。在这个fanpage.php我有一个表格,上面有我需要收集的姓名和邮件。 在他按下提交后,它会重定向到index.php,在那里验证输入(尚未完成),然后返回writetocsv.php?id = ..&amp; name = ..&amp; mail = ..其中我写数据进入csv后,它重定向回index.php。这是它被窃听的地方。它说OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。有什么建议吗?

编辑:

好吧,我自己想通了。对于任何有此问题的人来说,解决方案是:

Facebook仅在您第一次访问应用时发送签名请求。因此,您必须将signedrequest存储在会话中。至于访问令牌,您必须记住第一个访问令牌,并在每次通过$ facebook-&gt; setAccesToken($ accestoken)方法重新加载页面时设置它。祝你好运!

1 个答案:

答案 0 :(得分:0)

好吧,我自己想通了。对于任何有此问题的人来说,解决方案是:

Facebook 仅在您第一次访问该应用时发送signedrequest。因此,您必须将signedrequest存储在会话中。对于访问令牌,您必须记住第一个访问令牌,并在每次通过$facebook->setAccesToken($accestoken)方法重新加载页面时设置它。祝你好运!