mp3duck
mp3duck

Reputation: 2663

Facebook tab, finding if liked

I have the following code to check if my tab is liked by the user:

        protected bool IsPageLiked()
    {
        try
        {
            var current = ConfigurationManager.GetSection("facebookSettings")
                          as IFacebookApplication;
            dynamic data = FacebookWebContext.Current.SignedRequest.Data;

            if (data.page != null)
            {
                var pageId = (String)data.page.id;
                var isUserAdmin = (Boolean)data.page.admin;
                var userLikesPage = (Boolean)data.page.liked;
                if (userLikesPage)
                    return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
        return false;
    }

This works correctly when I load my tab initially. However, if I try and call the same code after changing page within the tab, I get the following error:

{"Precondition failed: !String.IsNullOrEmpty(signedRequestValue)"}

Is there a way I can make this code work after the first page?

I know I can check if the tab is liked on the first page, and put this into a session object or something, but I'd prefer not to do this.

My app itself is MVC 3

Thanks

EDIT

I think what is happening is when I change page (I'm using a RedirectToAction method), it's loosing the signed_request query string, hence the error I am getting.

Edit 2

Not sure the above is what is happening after all, as I can't see any query string values on the initial page? It's still not able to get the signedrequest.

It looks like the signed_request is a form object (Request.Form["signed_request"] returns the string in the inital page, but not the second page).

Upvotes: 0

Views: 594

Answers (1)

Jont
Jont

Reputation: 972

Page tabs are basically just your app within an iframe on the page, like you've noticed the signed request is POSTED to the tab on page load.

When you change page within the tab the outer Facebook frame isn't reloading so the signed_request is not being re-posted to your tab which is why you can't access it again.

You will need to either pass the signed_request from one page to the next yourself or make sure all links use target="_top" so that the whole page is reloaded each time and you still get the POST.

To define which page you want your tab to load you can use an additional parameter called app_data in the url to your tab, eg

http://www.facebook.com/MY_PAGE?sk=app_MY_APP_ID&app_data=A_STRING_OF_DATA

Your tab will then receive this as part of signed_request, you can grab it and use it to work out which page your tab needs to display.

Upvotes: 1

Related Questions