views:

1245

answers:

2

My site allow users to login via Facebook Connect and also allows them to update a status field our site. I want to include a checkbox below the status update field that will allow them to post this update to their Facebook profile as well if they so choose. In order to do this I need the user to grant our application permissions to post to their Facebook profile which is supported by prompting the user for 'publish_stream' permission using the JS function FB.Connect.showPermissionDialog http://developers.facebook.com/docs/?u=facebook.jslib.FB.Connect.showPermissionDialog.

The problem I'm having is figuring out if I need to prompt the user to do this. Basically the user experience I'd like to have is that when the user checks this box for the first time I want them to be prompted to grant this permission. Once that has been granted I won't need to prompt them in the future if they check that box again. I can't seem to figure out what JS to include in the onclick action of that checkbox that will check to see if the user has granted this permission and if not then prompt them, otherwise do nothing. Also after checking the box and granting the permission I want to do nothing (no page reload) but if they reject the request to grant permission I'd like the checkbox to be unchecked. I think I may need to use the FB.ApiClient.users_hasAppPermission JS function http://developers.facebook.com/docs/?u=facebook.jslib.FB.ApiClient.users_hasAppPermission but am still confused on what this would look like. The psuedo code for this would be:

onclick="
     if(user_already_granted_permission) { do_nothing; }
     else {
         prompt_user_to_grant_permission {
             wait_for_response_from_permission_dialog {
                 if(permission_granted) { do_nothing; }
                 else { uncheck_checkbox; }
             }
         }
     }
"

One other thing worth noting is that I've already verified the user is logged in through Facebook Connect when the page was rendered. I'm hoping someone else using Facebook Connect has already been able to figure this one out. Thanks in advance for your help!

+1  A: 

I recently developed an online demo that uses Facebook Connect and gets the friend list of a given user - it's not the same as your case, but I used FB.Facebook.apiClient.requireLogin to get the user logged, like this:

var api_key = 'd04fba62ff27c6c84a6b767d404bcec3';
var channel_path = '/xd_receiver.htm';

/* initialize facebook API */
FB_RequireFeatures(['Api'], function() {
    FB.Facebook.init(api_key, channel_path);

    var api = FB.Facebook.apiClient;

    api.requireLogin(function(exception){
        // user is logged in and my application
        // has permissions to get friend list
    });
});
Alexander Gyoshev
@Alexander - Thanks but the logged in part isn't the big issue I'm running into. Have you done any development where you've prompted users to publish to their news stream? If so that would be more applicable.
Russell C.
Sorry, I guess I didn't get the question right. I haven't used the permissions api, yet I guess the following post in the facebook developer forums should help you -- http://forum.developers.facebook.com/viewtopic.php?pid=124579#p124579
Alexander Gyoshev
A: 

You can do something like this:


FB.Facebook.apiClient.users_hasAppPermission('publish_stream', function(has_perms) {
    if(has_perms) {
        // User has granted the publish_stream permission
    } else {
        // User has not granted the publish_stream permission
    }
});

The second param for the users_hasAppPermission function is a callback which returns the result of the check.

mjangda