Facebook JS SDK: check permissions before logging in to PopUp

I want to check the permissions of the registered user before submitting the iframe name to request extended perms, but the iframe popup is blocked by the browser (ff, chrome checked). I want to avoid this, and I am quite sure of it, because the results of js-functions are "nested" - my smart mind is limited, so excuse me.

I assume that if I can save everything in the original function without passing the results, browsers will still see that the login iframe is a "user initiator" and not blocked.

But I can’t do this - for example, why the following result appears in data = undefined.

var data = FB.api( { method: 'fql.query', query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid }); alert(data); // = undefined 

My complete current code is:

 <button id="myButton" >Test Publish Event</button> <script> $('#myButton').bind('click', function() { FB.getLoginStatus(function(response) { if (response.session) { // logged in and connected user, someone you know FB.api( { method: 'fql.query', query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid }, function(response) { if(response[0].create_event == 1) { alert('permission granted'); } else { alert('permission absent'); FB.login( function(response) {if (response.session) { handleSessionResponse(response); if (response.perms) { // user is logged in and granted some permissions. // perms is a comma separated list of granted permissions alert(response.perms); } else { // user is logged in, but did not grant any permissions } } else { // user is not logged in } }, {perms:'create_event,rsvp_event'}); } } ); </script> 
+4
source share
2 answers

YAY! The same problematic problem (well, the same problem, but the same desired functionality) bothered me very much, but finally, after HOURS research on various sources, I understood the solution!

first

 var data = FB.api( 

It is not right! Since all fb requests are executed asynchronously, and the only way to get data values ​​is inside this request after it is executed. ex

 FB.api( { method: 'fql.query', query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid },function(response){ This function will run after the request is finished and ONLY inside here you can read the results data ex alert(response.data); }); } }); 

2. Your permission request is called blocked by chrome for security reasons (firefox allows other browsers to allow or not)

"A single popup can only be enabled if it is opened from a user action event such as .onclick." This way you can attach a function to the .onclick event.

3rd My solution to check if the user has the necessary permissions to use your application:

  FB.getLoginStatus(function(response) { if (response.status.toString().indexOf("connected")>-1) { initAll(); } else { requestPermisions proceedure } 

This function checks if the user is connected and has granted permissions for your application in another way. response.status = "unknown".

Now....

The problem with the popup permission block has 2 solutions.

1st solution = attach the FB.login () function to the onclick event of the .Ex button.

  ifUserNotgrandedPermisions{document.getElementByid("aLogInButton").onclick = function(){ FB.login(....blah blah blah); }; 

The second solution, and the one I implemented, redirects the iFrame to the request resolution page and does not pop up

Below is a complete solution that checks if a user is registered and provided with perms ... if he does not ask the user to log in and then ask for permissions (if you are logged in, just ask perms) (if he has only logs )

  FB.init({appId: 'YourAPPID', status: true, cookie: true, xfbml: true, oauth : true}); FB.getLoginStatus(function(response) { if (response.status.toString().indexOf("connected")>-1) { initAll(); //User is connected and granted perms Good to go! } else { 

//top.location changes the browser url and NOT the iframe url

// This url was specifically created for the perms request for your application. You change permissions and your appID

// redirect_uri = Change this to the application canvas page

  top.location=window.location="http://www.facebook.com/dialog/oauth/?scope=read_stream,publish_stream,friends_photos,friends_activities&client_id="yourAPPID(nobrackets)"&redirect_uri=http://apps.facebook.com/filtered_feed/&response_type=code";}});}; 
+8
source
 FB.init({ appId: 'YOUR APP ID', status: true, cookie: true, xfbml: true, oauth : true }); FB.getLoginStatus(function(response) { if (response.status === 'connected') { /*var uid = response.authResponse.userID; //FACEBOOK_ID var accessToken = response.authResponse.accessToken;*/ //ACCESS TOKEN FB.Canvas.setAutoResize(); if (response.authResponse) { // logged in and connected user, someone you know. // YOUR SUCCESS CODE HERE } }else { attemptLogin(); } }); 

Function to open a pop-up window for logging into facebook and for authorization.

 function attemptLogin(){ FB.login(function(response) { if (response.authResponse) { login(); } else { //if user didn't logged in or didn't authorize your application } }, {scope: 'offline_access,publish_stream,manage_pages'}); //YOUR PERMISSION } 
+1
source

All Articles