Question

Is it possible to authenticate a user from a tab app?

I am trying to create an ASP.NET MVC 3 facebook tab app that needs to be able to post to wall.

My first guess was using the CanvasAuthorizeAttribute

[CanvasAuthorize(Permissions="publish_stream")]

but it makes my tab redirect to the the login facebook authorization page and then back to the canvas page and not staying in the fanpage (tab).

Then I remove the CanvasAuthorize and tried the following

public string Index()
{
    bool isAuthorized = FacebookWebContext.Current.IsAuthorized("publish_stream");

    return isAuthorized.ToString();
}

from that I could decide to launch the authorize popup if the user is not authorized, but I am facing a problem here.

  • Previously authorizing the app with stream_publish permission by other means and then, running the page as an app ( http://apps.facebook.com/myapp/mytab ) returns true while running the page from a fanpage tab ( http://www.facebook.com/myfanpage?sk=app_myappid ) returns false !

Why can't the tab read the permission but the canvas page can?

Was it helpful?

Solution

From you Facebook Page, you can detect whether the user has authorized your app or not by examining the signed_request. If user_id and oauth_token are passed, then the user has already authorized your app. If not, you can do something like this from your Controller Action:

    string[] extendedPermissions = new[] { "publish_stream", "manage_pages" };
    var oauth = new FacebookOAuthClient(FacebookWebContext.Current.Settings);
    var parameters = new Dictionary<string, object> {
                { "redirect_uri", "http://www.facebook.com/pages/{SomeFacebookPage}?sk=app_{appId}"}
            };

    parameters["scope"] = String.Join(",", extendedPermissions);
    var loginUrl = oauth.GetLoginUrl(parameters);

    return Redirect(loginUrl.AbsoluteUri);

After your user authenticates your app, the user will be sent to the "redirect_uri", which can be your app tab on a Facebook Page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top