2012-01-22 7 views
0

認証でリダイレクトuriの処理に問題があります。私は自分のサイトにそれを設定した場合facebookがfacebookの外にある私のサイトにリダイレクトしています

$_Request['code']が設定されているため、ユーザは、認証を行いますが、その後、ユーザーが自分のサイトになります、と私は

は私がapps.facebookにリダイレクトした場合ことをしたくありません.com/myappの場合、$_Request['code']は設定されておらず、ユーザーは認証されず空白のページが表示されます。

PHPでこれを行う方法はありますか?ページがレンダリングされる前にコードが実行されています。

どうやってこの問題を解決しますか?

私のログイン機能は:

public static function login($redirect) { 
    $app_id = AppInfo::appID(); 
    $app_secret = AppInfo::appSecret(); 
    $home = urlencode(AppInfo::getHome()); 
    // See https://developers.facebook.com/docs/reference/api/permissions/ 
    // for a full list of permissions 
    $scope = 'user_photos,publish_stream'; 
    session_start(); 
    $code = $_REQUEST["code"]; 
    // If we don't have a code returned from Facebook, the first step is to get 
    if (empty($code)) { 
     // CSRF protection - for more information, look at 'Security Considerations' 
     // at 'https://developers.facebook.com/docs/authentication/' 
     $state = md5(uniqid(rand(), TRUE)); 
     setcookie(
     AppInfo::appID() . '-fb-app', 
     $state, 
     $expires = 0, 
     $path = "", 
     $domain = "", 
     $secure = "", 
     $httponly = true); 
     // Now form the login URL that you will use to authorize your app 
     $authorize_url = "https://www.facebook.com/dialog/oauth?client_id=$app_id" . 
     "&redirect_uri=$home&state=" . $state . "&scope=$scope"; 
     // Now we redirect the user to the login page 
     echo("<script> window.location.href='" . $authorize_url . "'</script>"); 
     return false; 
    // Once we have that code, we can now request an access-token. We check to 
    // ensure that the state has remained the same. 
    } else if ($_REQUEST['state'] === $_COOKIE[AppInfo::appID() . '-fb-app']) { 
     $ch = curl_init("https://graph.facebook.com/oauth/access_token"); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, 
     "client_id=$app_id&redirect_uri=$home&client_secret=$app_secret" . 
     "&code=$code&scope=$scope"); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $response = curl_exec($ch); 
     // Once we get a response, we then parse it to extract the access token 
     parse_str($response, $params); 
     $token = $params['access_token']; 
     return $token; 
    // In the event that the two states do not match, we return false to signify 
    // that something has gone wrong during authentication 
    } else { 
     echo("States do not match. CSRF?"); 
     return false; 
    } 
    } 
+0

@mogulzalpは、PHPでこれを行うにはどのような方法がある参照してください、私はコードページがレンダリングされる前に実行している、それはユーザー – Jakob

+0

@moguzalpに少ないロード時間を意味します - 大丈夫。あなたの答えをありがとう、それは私が今まで持っている最高です:) – Jakob

+0

@ヤコブ私はyurのフルコードを見ることができますか? どのような目的のために、そのコードを使用していますか? – Felix

答えて

0

あなたがFacebook上で自分のアプリを持っているしたい場合、あなたは自分のキャンバスURLへsigned_requestパラメータFacebookの投稿を使用することができます。ユーザーがアプリを承認したら、codeを読む必要はありません。現在のユーザーがあなたのアプリを承認していなくても、Facebookは常にこのパラメータを送信することに注意してください。

documentation

関連する問題