Geeks With Blogs
The Unstable Mind of a .Net Developer

In my previous post on this particular topic, I had mentioned a problem I was having when attempting to post with a new application.

I did discover that not all applications are created equal.  When I created a second application specifically for this post, it would not work at all.  I have not yet contacted Facebook support to determine the root cause.  If you run into the issue where it appears that the application is being granted the proper permissions, but still cannot post, create another application and try it.

It turns out that there was, in fact, a larger issue to be surfaced.  Shortly after I completed this project, Facebook changed some of their APIs which then broke some of the RESTful APIs that are used by the Microsoft Facebook Developer Toolkit.  So, the following code no longer works.

// Request desired permissions.  If only updating status or content, need publish_stream
List<Enums.ExtendedPermissions> lst = new List<Enums.ExtendedPermissions>();
lst.Add(Enums.ExtendedPermissions.status_update);
session.RequiredPermissions = lst;

Instead, we must leverage some of the new Facebook Connect API methods to request permissions from the user.  There are only a couple of changes necessary.  First, remove the above code.  It will be replaced with a call to FB.Connect.showPermissionDialog.  Next, instead of reloading the browser window as part of the fb login-button control, invoke a new callback function.  We will define this function in a moment.

pnlFacebookConnect.Controls.Add(
    new Literal { Text = "<fb:login-button onlogin='ToInvoke.CallBack()'></fb:login-button>" });

 

Lastly, we need to define and emit the new callback function.  With its complexity, it is easier to see and read if we define it as a constant else where in the code.

 

Define the callback.  Note the call to showPermissionDialog.  Add the necessary permissions to the string delimited by commas.

        private const string LoginCallBackScript = @"
ToInvoke = {};
FB.ensureInit(function() {
ToInvoke.CallBack=function ()
{
    FB.ensureInit(
    function()
    {
        FB.Connect.showPermissionDialog(
        'publish_stream,status_update',  
        function(perms)
        { 
           window.location.reload();
        });  
    });
}
});";

 

 

And emit the code.

ClientScript.RegisterStartupScript(this.GetType(), "facebook_init", 
    string.Format("<script type='text/javascript'>FB.init('{0}', 'xd_receiver.htm'); {1} </script>", 
    APPLICATION_ID, LoginCallBackScript));

 

With that, our application should be up and running with the proper permissions.  One of the positives about this particular method is that it always checks for the requested permissions.  If they have been granted, no foul.  If not, or if the requested permissions have changed, it will re-prompt the user for the updated permissions.  So, our application and permissions should always be in sync.

 

I’ve uploaded an updated project to Windows SkyDrive here.

 

<edit - 08/09/2011>

One of the unfortunate side effects of the new Facebook APIs is that when asking for permission, first the user is asked to confirm basic permissions and then any additional permissions.  This cannot be avoided.

</edit>

 

Ralph Wheaton
Microsoft Certified Technology Specialist
Microsoft Certified Professional Developer

Posted on Friday, September 24, 2010 9:16 AM Facebook , Social Media | Back to top


Comments on this post: Social Media 101A: Facebook Connect Redux

# re: Social Media 101A: Facebook Connect Redux
Requesting Gravatar...
Hi Ralph,

Your post is very helpful but got into one issue, Can you please help me?

I used the way u said, it works half way. At first it gives a FLogin button. On Click, it opens a popup, where we enter the email id and password. After that it opens a window where it asks for the basic permission to be alloed and when I click allow there, then it opens another popup with all other permissions I give in. So it gives out 2 permission request popups. Can we make that as one? Can you please help me in this regard?

Thanks,
Arun S.
Left by Arun on Oct 08, 2010 6:55 AM

Your comment:
 (will show your gravatar)


Copyright © Ralph Wheaton | Powered by: GeeksWithBlogs.net