0

私はいくつかのサブドメイン Web サイトを持っており、それぞれに Facebook を接続したいと考えています。

メイン ドメイン用に fb アプリを作成しましたが、それで動作します。

各サブドメインで、このリンクを使用して接続します(ajax呼び出しによって作成されました):

<?php
echo "<a href=\"https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=myID
    &scope=email,publish_stream,status_update&redirect_uri=http://www.mydomain.com/fbConnect.php?ref=".$_SERVER['HTTP_REFERER']."\">
    Connect with Facebook
    </a>";
?>

私の fbConnect.php

<?php
    header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); // Hack IE for POST params...
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");

    session_set_cookie_params(0, '/', '.mydomain.com', false); // If session is lost with subdomains...
    session_start();
    require('/home/....../facebook.php');

    $facebook = new Facebook(array(
    'appId' => 'myid',// changed for the example
    'secret' => 'mysecret', // same
    'cookie' => true,
    ));

    $user = null;

    $loginUrl=$facebook->getLoginUrl(
            array(
                  'canvas'    => 0,
                'scope'         => 'email,publish_stream,user_location'
            )
    );
    $logoutUrl  = $facebook->getLogoutUrl();

    $user=$facebook->getUser();

    if(!$user) echo "<script>top.location.href='".$login_url."'</script>";

    if ($user) {
        echo "Ok";
        $user_profile = $facebook->api('/me');  
        $userInfo = $facebook->api("/$user");
        $_SESSION['fb_id']=$userInfo['id'];
       // Some stuff...
       echo "<script type='text/javascript'>top.location.href = '".$_GET['ref']."';</script>";
    }
?>

$_SESSION['fb_id']スコープ、接続、およびリダイレクトは機能していますが、ページに戻ることができません$_GET['ref']...ただし、session_id() は同じです!

4

1 に答える 1

1

最後に、いくつかの間違いがありました:

1. セッション

彼らはサブドメインでフォローしていなかったので、秘訣はphp.iniを変更するか追加することini_set("session.cookie_domain", ".myDomain.com");でした(@Tommy Crushに感謝します)

2.redirect_uri

私の場合、変数をredirect_uri=http://www.myDomain.com?var1=123&var2=456...

3. API の使用

GRAPH API の新しい初心者用パネルを読まなければならなかったのですが、変更点の多さに驚きました...

私は最終的に以下を使用しました:

サブドメインごとに

// Simple link to the connection page
echo "<a href=\"http://wwww.myDomain.com/fbConnect.php\">Connect with FB</a>";
// Record the current page whitch called this ajax
$_SESSION['connexion_ref']=$_SERVER['HTTP_REFERER'];

私の新しい fbConnect.php

ini_set("session.cookie_domain", ".myDomain.com");
session_start();

$app_id = "myappid";
$app_secret = "myappsecret";
$my_url = "http://www.mydomain.com/fbConnect.php";


$code = $_REQUEST["code"];

if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
 $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
   . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
   . $_SESSION['state']. "&scope=email,publish_stream,status_update,offline_access";

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) 
{
    $token_url = "https://graph.facebook.com/oauth/access_token?"
     . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
     . "&client_secret=" . $app_secret . "&code=" . $code;

    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);


    $_SESSION['access_token'] = $params['access_token'];

    $graph_url = "https://graph.facebook.com/me?access_token=" 
     . $params['access_token'];

    $user = json_decode(file_get_contents($graph_url));
    //var_dump($user);


    $_SESSION['id_fb']=$user->id;
   // Some stuff
    // Then redirect to the subdomain page of connection
   echo "<script type=\"text/javascript\">top.location.href =\"".$_SESSION['connexion_ref']."\";</script>";
}

今では魅力のように機能します。

于 2013-01-03T17:07:31.637 に答える