Facebook使用JavaScript连接

时间:2012-03-02 03:12:02

标签: javascript facebook

要连接到Facebook,我已将其放在正文标记中:

<body>
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'xxxxxxx',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
          });
        };
        (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 class="fb-login-button">Login with Facebook</div>
    </body>

我遇到的唯一问题是如何知道用户是否已登录?

3 个答案:

答案 0 :(得分:1)

使用FB.getLoginStatus确定用户是否已登录。

答案 1 :(得分:1)

我希望这对你有所帮助

function login() {

        FB.login(function(response) {
            if (response.authResponse) {
                alert('Success!');
            }else{
                alert('Login Failed!');
            }
        }, {scope: 'email'});
     }

答案 2 :(得分:1)

/*********************
 *
 * Class Name: FacebookConnect
 *
 * Description:
 *
 * Version: 1.5
 *
 * @param autologin
 * @param initCallback
 * @returns {FacebookConnect}
 */


function FacebookConnect(autologin, initCallback) {

        this.inviteFriends = inviteFriends;
        this.addLikeEvent = addLikeEvent;
        this.getMe = getMe;
        this.oauth = oauth;
        this.query = query;
        this.sendMensage = sendMensage;
        this.publish = publish;
        this.login = login;
        this.AppId = AppId;
        this.share = share;
        this.feed = feed;
        this.getAlbums = getAlbums;
        this.getPhotoSrc = getPhotoSrc;
        this.getPhotoAlbums = getPhotoAlbums;
        this.getFriends = getFriends;
        this.getUser = getUser;
        this.createEvent = createEvent;
        this.isFan = isFan;
        this.postLike = postLike;

        this.autologin = autologin;

        this.fbStatus = fbStatus;

        /******** Config Params ********/

        var AppId = '55343433434344'; // App ID/API Key
        var perms = 'publish_stream,email,user_likes'; // Scope to access data
        var AppUrl = 'https://apps.facebook.com/sencillamentetu/';

        var fbStatus = false;

        window.fbAsyncInit = function() {

                FB._https=true;

                FB.init({
                        appId : AppId,
                        xfbml: true,
                        status : true,
                        cookie : true,
                        oauth : true
                });

                /* All the events registered */
                FB.Event.subscribe('auth.login', function(response) {
                        // do something with response
                });
                FB.Event.subscribe('auth.logout', function(response) {
                        this.fbStatus = false;
                });

                FB.getLoginStatus(function(response) {


                        if (response.status === 'connected') {

                                fbStatus = true;
                                initCallback(fbStatus);

                         } else if (response.status === 'not_authorized') {

                                fbStatus = false;

                                if (autologin != false) {
                                        login(initCallback);
                                }else{
                                        initCallback(fbStatus);
                                }
                         } else {

                                fbStatus = false;

                                if (autologin != false) {
                                        login(initCallback);
                                }else{
                                        initCallback(fbStatus);
                                }
                         }
                });

                FB.Event.subscribe('edge.create', function(href, widget) {
                    likeCallback();
                });

        };
         (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));

        function addLikeEvent(event,callback){

                if(event == "like"){

                        FB.Event.subscribe('edge.create', function(response, widget) {
                                callback(response);
                        });
                }
        }

        function login(callback) {
                if (fbStatus == true) {
                        callback(true);
                } else {
                        try {
                                FB.login(function(response) {
                                        if (response.authResponse) {
                                                /* User is login */
                                                fbStatus = true;
                                                callback(true);
                                        } else {
                                                /* User cancelled login or did not fully authorize. */
                                                fbStatus = false;
                                                callback(false);
                                        }
                                }, {
                                        scope : perms
                                });
                        } catch (e) {

                        }
                }

        }

        function oauth() {
                try{
                        FB.ui({
                                method : 'oauth',
                                client_id : AppId,
                                redirect_uri : AppUrl,
                                scope : perms,
                                display : 'page'
                        }, function(response) {
                                if (callback) {
                                        callback(response);
                                }
                        });
                } catch(e) {
                        //console.log(e);
                }
        }

        function getMe(callback) {

                login(function(response) {
                        if (response == true) {
                                FB.api('/me', function(response) {
                                        if (callback) {
                                                callback(response);
                                        }
                                });
                        }
                });

        }

        function postLike(objectToLike,callback) {
                login(function(response) {
                        if (response == true) {
                            FB.api(
                                       'https://graph.facebook.com/me/og.likes',
                                       'post',
                                       { object: objectToLike,
                                         privacy: {'value': 'SELF'} },
                                       function(response){
                                                        if (callback){
                                                                callback(response);
                                                        }
                                       }
                                    );
                        }
                });
          }

        function inviteFriends(msg,callback) {

                login(function(response) {

                                if (response == true) {
                                        FB.ui({
                                                        method : 'apprequests',
                                                message : msg
                                        },function(response) {
                                        if (response && response.request_ids) {
                                                if (callback) {
                                                        callback(response.request_ids);
                                                }
                                        }
                                });
                        }
                });

        }

        function sendMensage(message, to, link, picture, description, callback) {

                login(function(response) {
                        if (response == true) {
                                FB.ui({
                                        method : 'send',
                                        name : message,
                                        to : to,
                                        link : link,
                                        picture : picture,
                                        description : description
                                }, function(response) {
                                        if (callback) {
                                                callback(response);
                                        }
                                });
                        }
                });

        }

        function getFriends(callback) {


                var friendIds = new Array();


                login(function(response) {
                        if (response == true) {
                                FB.api({
                                        method : 'friends.get'
                                }, function(response) {


                                        friendIds = response;


                                        var fql = "SELECT uid,pic_square,pic_big,name FROM user WHERE uid in("+friendIds+") ORDER BY name ASC";
                                FB.api(
                                                        {
                                                                method: 'fql.query',
                                                                query: fql
                                                        },
                                                        function(response) {


                                                                callback(response);



                                                        }
                                        );
                                });
                        }
                });

        }

        function getAlbums(callback) {

                login(function(response) {
                        if (response == true) {
                                FB.api('/me/albums', function(response) {
                                        if (callback) {
                                                callback(response);
                                        }
                                });
                        }
                });

        }


        function publish(attachment, action, message, target_id, callback) {

                login(function(response) {
                        if (response == true) {
                                //console.log("Publishing");
                                FB.ui({
                                        method : 'stream.publish',
                                        message : message,
                                        attachment : attachment,
                                        action_links : action,
                                        user_prompt_message : '',
                                        target_id : target_id,
                                        callback : callback(response)
                                });
                        }
                });

        }

        function feed(name, link, img, caption, description, callback) {

                login(function(response) {
                        if (response == true) {
                                FB.ui({
                                            method: 'feed',
                                            name: name,
                                            link: link,
                                            picture: img,
                                            caption: caption,
                                            description: description
                                        }, function(response) {
                                            if (callback) {
                                                        callback(response);
                                                }
                                        });
                        }
                });

        }

        function share(link, message, callback){

                  var share = {
                                  method: 'stream.publish',
                                  link: link,
                                  message: message,
                  };


                FB.ui(share, function(response) {
                                if (callback) {
                                        callback(response);
                                }
                });
            }

        function friendsAdd(idUser, callback) {

                login(function(response) {
                        if (response == true) {
                                FB.ui({
                                        method : 'friends.add',
                                        id : idUser
                                }, function(response) {
                                        if (callback) {
                                                callback(response);
                                        }
                                });
                        }
                });

        }

        function query(queryString, callback) {

                login(function(response) {
                        if (response == true) {
                                FB.api({
                                        method : 'fql.query',
                                        query : queryString
                                }, function(response) {
                                        if (callback) {
                                                callback(response);
                                        }
                                });
                        }
                });

        }

        function getUser(idUser,callback) {
                login(function(response) {
                        if (response == true) {

                        var fql = 'SELECT uid,name,pic_square,email FROM user WHERE uid ='+idUser;
                        FB.api(
                                                {
                                                        method: 'fql.query',
                                                        query: fql
                                                },
                                                function(response) {
                                                        callback(response);
                                                }
                                );
                        }
                });
        }

        function getAlbums(idUser,callback){


                login(function(response) {
                        if (response == true) {

                        var fql = 'SELECT aid,owner,cover_pid,name,size FROM album WHERE owner ='+idUser;
                        FB.api(
                                                {
                                                        method: 'fql.query',
                                                        query: fql
                                                },
                                                function(response) {
                                                        callback(response);
                                                }
                                );
                        }
                });


    }
    function getPhotoAlbums(idAlbum,callback){


                login(function(response) {
                        if (response == true) {

                        var fql = "SELECT pid FROM photo WHERE aid="+idAlbum;
                        FB.api(
                                                {
                                                        method: 'fql.query',
                                                        query: fql
                                                },
                                                function(response) {
                                                        callback(response);
                                                }
                                );
                        }
                });

    }
    function getPhotoSrc(idPhoto,callback){


                login(function(response) {
                                if (response == true) {

                                var fql = "SELECT src_small,src,src_big FROM photo WHERE pid="+idPhoto;
                                FB.api(
                                                        {
                                                                method: 'fql.query',
                                                                query: fql
                                                        },
                                                        function(response) {
                                                                callback(response);
                                                        }
                                );
                        }
                });

    }

    function createEvent(name, startTime, endTime, location, description, callback) {
            /*"access_token": fbtoken,*/
            FB.api("/me/events",'post',{
                        start_time:startTime,
                        end_time:endTime,
                        location:location,
                        name:name,
                        description:description,
                        privacy:'OPEN'
                    },
                    function(response){
                        callback(response);
                }
            );
        }

        function isFan(page_id, callback){

        login(function(response) {
           if (response == true) {
                        FB.api('/me/likes/'+page_id,function(response) {
                    if( response.data ) {
                        if( !isEmpty(response.data) )
                            callback(true);
                        else
                            callback(false);
                    } else {
                        callback("error");
                    }


                        });
                    }
        });

        }

    this.setSize = function(data){

    if(data){
                FB.Canvas.setSize(data);
                FB.Canvas.scrollTo(0,0);
    }else{
                FB.Canvas.setAutoGrow(100);
    }



    }

        // function to check for an empty object
        function isEmpty(obj) {
            for(var prop in obj) {
                if(obj.hasOwnProperty(prop))
                    return false;
            }

            return true;
        }


}