简体   繁体   中英

How to Reuse Facebook Access token to login using javascript sdk

I need to implement Facebook Connect With javascript sdk,Right now,I am able to Allow users to Login into my website using their facebook credentials..but the problem is Everytime the users visit my page it asks for facebook login..is it possible that already logged in users to allow to post,Share and access their profile details and friends list etc..automatically...Can anyone help me out with this.....Thank u in advance.. Currently My code is like this...

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

                            // Init the SDK upon load
                            window.fbAsyncInit = function () {
                                FB.init({
                                    appId: 'xxxxxxxxxxxxx', // App ID 
                                    channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File 
                                    status: true, // check login status 
                                    cookie: true, // enable cookies to allow the server to access the session 
                                    xfbml: true  // parse XFBML 
                                });

                                // listen for and handle auth.statusChange events 
                                FB.Event.subscribe('auth.statusChange', function (response) {
                                    if (response.authResponse) {

                                        // user has auth'd your app and is logged into Facebook 
                                        FB.api('/me', function (me) {
                                            if (me.name) {
                                                document.getElementById('auth-displayname').innerHTML = me.name;
                                                document.getElementById('UserEmail').innerHTML = me.email;
                                                document.getElementById('UserGender').innerHTML = me.gender;
                                                document.getElementById('AccessToken').innerHTML = response.authResponse.accessToken;

                                                getPhoto();
                                            }
                                        })
                                        document.getElementById('auth-loggedout').style.display = 'none';
                                        document.getElementById('auth-loggedin').style.display = 'block';
                                    } else {
                                        // user has not auth'd your app, or is not logged into Facebook 
                                        document.getElementById('auth-loggedout').style.display = 'block';
                                        document.getElementById('auth-loggedin').style.display = 'none';
                                    }
                                });
                                $("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });


                                function getPhoto() {
                                    FB.api('/me/picture?type=normal', function (response) {

                                        var str = "<br/> <img src='" + response.data.url + "'/>";

                                        document.getElementById("ProfilePic").innerHTML = str;

                                    });
                                }
                            }

                        </script>

这太复杂了,您可以只使用FB.getLoginStatus而不是FB.Event.subscribe ,它会自动刷新令牌: https : //developers.facebook.com/docs/reference/javascript/FB.getLoginStatus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM