2011-10-23 13 views
4

私は自分のサイトのホームページに基本的なログイン/認証フローを設定しました。認証ダイアログの後、サイトの別のページにリダイレクトしたいと思います。私は既存のコードのいくつかのバリエーションを試してきました。デベロッパーアプリケーションの基本設定でサイトURLを変更し、異なるoauthフローを試していましたが、ログインした同じホームページにリダイレクトするだけです。私はこれが非常に簡単に聞こえることは知っていますが、非常に多くの異なる認証フローと実装が混乱しています。認証後にJavascript SDKを使用してリダイレクトする

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

    // Additional initialization code here 
    }; 

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

そして、いくつかのより多くの楽しみ...

<div id="fb-root"></div> 
<div id="user-info"></div> 
<button id="fb-auth">Login</button> 

<script> 
window.fbAsyncInit = function() { 
FB.init({ appId: 'XXXXXXXXXXXXXXXXXXXXXX', 
    status: true, 
    cookie: true, 
    xfbml: true, 
    oauth: true}); 

function updateButton(response) { 
var button = document.getElementById('fb-auth'); 

if (response.authResponse) { 
    //user is already logged in and connected 
    var userInfo = document.getElementById('user-info'); 
    FB.api('/me', function(response) { 
    userInfo.innerHTML = '<img src="https://graph.facebook.com/' 
    + response.id + '/picture">' + response.name; 
    button.innerHTML = 'Logout'; 
    }); 
    button.onclick = function() { 
    FB.logout(function(response) { 
     var userInfo = document.getElementById('user-info'); 
     userInfo.innerHTML=""; 
}); 
    }; 
} else { 
    //user is not connected to your app or logged out 
    button.innerHTML = 'Login'; 
    button.onclick = function() { 
    FB.login(function(response) { 
    if (response.authResponse) { 
     FB.api('/me', function(response) { 
     var userInfo = document.getElementById('user-info'); 
     userInfo.innerHTML = 
      '<img src="https://graph.facebook.com/' 
     + response.id + '/picture" style="margin-right:5px"/>' 
     + response.name; 
    });  
     } else { 
     //user cancelled login or did not grant authorization 
     } 
    }, {scope:'email,user_birthday'});  
    } 
} 
} 

    // run once with current status and whenever the status changes 
    FB.getLoginStatus(updateButton); 
    FB.Event.subscribe('auth.statusChange', updateButton);  
}; 

(function() { 
var e = document.createElement('script'); e.async = true; 
e.src = document.location.protocol 
+ '//connect.facebook.net/en_US/all.js'; 
document.getElementById('fb-root').appendChild(e); 
}()); 
</script> 

と関係のない別の問題。なぜ私は通常のボタンポップアップしか持っていないのですか?単純なログインボタンではなく、Facebookログインボタンを表示するためにSDKを入手するにはどうすればよいですか?

答えて

4

ログイン応答後にユーザーをリダイレクトできる必要があります。あなたはログインボタンをしたい場合は、うーん...よく、あなたは、HTMLで簡単なログインボタンを書いている

window.top.location = "http://page_to_redirect"; 

ボタンの事:これらの行の下:

FB.login(function(response) { 
    if (response.authResponse) { 

このような何かを追加これに ログイン

<fb:login-button></fb:login-button> 

がそれを願って、これを変更助けてください。

+0

+1しかし、どのように私はそこaccess_tokenはを得ることができますか?私はこのような何かをしたいと思っています 'window.top.location =" http:// page_to_redirect&access_token = ?????; ' –

+1

authResponseオブジェクトにはアクセストークンが付属しています: var accessToken = authResponse.accessToken; – Chuy

関連する問題