Redirect back to page tab after user authenticates?

筅森魡賤 提交于 2020-01-21 05:31:08

问题


How should I go about redirecting the user back to my page's tab after they authenticate my app? I cannot put one specific url in for the redirect since my app will live on multiple pages. So somehow I need to grab the page's id and put it into the url. I've tried to use session variables but it doesn't seem to be working for me. :( Here's a portion of my code...

$signed_request = $facebook->getSignedRequest();

$_SESSION['TrueID'] = $signed_request['page']['id']; 

$fbconfig['appBaseUrl'] = "http://www.facebook.com/pages/".$_SESSION['TrueID']."/".$_SESSION['TrueID']."?sk=app_241321439259320";

/* 
 * If user first time authenticated the application facebook
 * redirects user to baseUrl, so I checked if any code passed
 * then redirect him to the application url 
 * -mahmud
 */
if (isset($_GET['code'])){
    header("Location: " . $fbconfig['appBaseUrl']);
    exit;
}
//~~

//
if (isset($_GET['request_ids'])){
    //user comes from invitation
    //track them if you need
}

As you can see I'm trying to set a session variable to grab the page's id.. but that's not working for me :( The variable echo's out just fine when I visit my page.. but I'm guessing its getting lost somewhere during the authentication.


回答1:


So when a Page add/install your app, you should store the page's link along with the page's id.

Now when your page tab is loaded, Facebook will send the page parameter which will contain the page id (along with other info, refer to the documentation). You retrieve that id, get the page's link from your db and construct your page tab link, which would be something like (where $page is the page's db record):

$redirect_uri = $page['page_link'] . '?sk=app_' . $APP_ID

Since you are using the PHP-SDK, this is how you construct your login:

$user = $facebook->getUser();
if(!$user) {
    $url = $facebook->getLoginUrl(array('redirect_uri' => $redirect_uri, 'scope' => 'read_stream'));
    echo "<script>top.location.href = '$url';</script>";
    exit;
}

Of course you may not want to redirect to the login directly but instead have a call to action link:

<a href="<?php echo $url;?>" target="_top">Connect</a>



回答2:


Best way I found is to set the Site URL in the app settings to http://www.facebook.com/pages/

Then do something like this(tested):

$uid = '';

$facebook = new Facebook(array(
        'appId'  =>'xxxxxxxxxxxxxxx',
        'secret' =>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'cookie' => true,
    'oauth'  => true,
        ));

$uid = $facebook->getUser();

$signedrequest = $facebook->getSignedRequest();

$page_id = $signedrequest["page"]["id"]; 

$fb_access_token = $signedrequest["oauth_token"];

if($uid==''){
echo '<div id="authenticate"><a href="https://www.facebook.com/dialog/oauth/?client_id=APP_CLIENT_ID_HERE&redirect_uri=http://www.facebook.com/pages/'. $page_id .'/'.$page_id.'%3Fsk%3Dapp_APP_CLIENT_ID_HERE&response_type=token" target="_top">Authorize App</a></div>';

This might be a little bit of a hack, but it works well for my situation:) Hope it helps someone!



来源:https://stackoverflow.com/questions/8158657/redirect-back-to-page-tab-after-user-authenticates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!