2012年6月15日 星期五

FB JS SDK-使用者登入FB APP

不知道是不是閏月的關係~ 最近真是太血淚了Orz..

在Java Base, FB沒有特製的SDK, 就只能用JS SDK
主要有Issue要判斷~ facebook的user是不是某某粉絲團的粉絲~
所以要先弄一個FB APP去access使用者資訊..

而如何讓使用者登入FB APP並同意隱私權授權..
不換頁~ 直接在同一頁登入..
在官網的範例有很不錯的範例~ 我只是把另開改成在原本頁面處理...
FB.api + oauth dialogs
http://developers.facebook.com/docs/reference/dialogs/oauth/

---html/jsp include FB js lib
 <script src="http://connect.facebook.net/zh_TW/all.js"></script>

 ---js script/file
FB.init({
    appId: '{APP_ID}', // App ID
    //channelURL: '....................', // Channel File
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    oauth: false, // enable OAuth 2.0
    xfbml: true  // parse XFBML
});

function loginUser() {
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            //login and permit APP access before
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            //do something when user login FB APP ok
            $('#like_box').css('display', 'block'); 
        } else if (response.status === 'not_authorized') {
            //login but not access app
            doLoginApp2();
        } else {
            // the user isn't logged in to Facebook.
            doLoginApp2();           
        }
    }, true);
}


function doLoginApp2()  {
    var appID = '{APP_ID}';
   
    var path = 'https://www.facebook.com/dialog/oauth?';
    //on moblie, display=touch
    var queryParams = ['client_id=' + appID, 'redirect_uri=' + window.location, 'response_type=token', 'display=touch'];
    //on PC web
    //var queryParams = ['client_id=' + appID, 'redirect_uri=' + window.location, 'response_type=token'];
    var query = queryParams.join('&');
    var url = path + query;
       
    window.location = url;
}

沒有留言: