facebook javascript sdk - FB.login inside FB.getLoginStatus popup window blocked -
i'm trying integrate facebook login website facebook javascript sdk. according step step instructions provided facebook developer document here, here test code wrote:
<script> window.fbasyncinit = function() { fb.init({ appid : '{$my_app_id}', cookie : true, // enable cookies allow server access session xfbml : true, // parse social plugins on page version : 'v2.1', // utilize version 2.1 status : true, // check fb login status }); }; function fblogin() { fb.getloginstatus(function(response) { if (response.status === 'connected') { alert('logged in.'); } else { fb.login(); } }, true); } </script> <button onclick="fblogin()">login</button> <script src="//connect.facebook.net/en_us/sdk.js"></script>
sorry, because of restriction of website domain, can't take fiddle. tried on own domain, facebook login window popuped in chrome , firefox, blocked safari. kind of weird, there wrong code snippets provided facebook developer document?
as discussed in comments, fb.login must called on user interaction, illustration in docs not correct.
this should working illustration how login correctly, copied several articles in facebook docs:
class="lang-js prettyprint-override"><script> //call on user interaction (click) function dologin() { fb.login(function(response) { if (response.authresponse) { console.log('welcome! fetching information.... '); fb.api('/me', function(response) { console.log('good see you, ' + response.name + '.'); }); } else { console.log('user cancelled login or did not authorize.'); } }, {scope: 'email,user_friends'}); } window.fbasyncinit = function() { fb.init({ appid : 'your-app-id', xfbml : true, version : 'v2.1' }); fb.getloginstatus(function (response) { if (response.status === 'connected') { // user logged in , has authenticated // app, , response.authresponse supplies // user's id, valid access token, signed // request, , time access token // , signed request each expire var uid = response.authresponse.userid; var accesstoken = response.authresponse.accesstoken; } else if (response.status === 'not_authorized') { // user logged in facebook, // has not authenticated app } else { // user isn't logged in facebook. } }); }; (function(d, s, id){ var js, fjs = d.getelementsbytagname(s)[0]; if (d.getelementbyid(id)) {return;} js = d.createelement(s); js.id = id; js.src = "//connect.facebook.net/en_us/sdk.js"; fjs.parentnode.insertbefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> <button onclick="dologin()">login</button>
more explanation: http://www.devils-heaven.com/facebook-javascript-sdk-login/
facebook-javascript-sdk popupwindow
No comments:
Post a Comment