0
votes

I'm using the Facebook C# SDK v5.3.2 to build a facebook canvas application. I'm not using any clientside authentication I'm doing it all server side. I've created an application using ASP.NET MVC 3 and my canvas page resides on www.mydomain.com/FacebookCanvas/. The application works great once a user has authenticated with it. However a problem arises with the first time the user authenticates with it. There is a problem redirecting the user to the correct url after authentication has been confirmed. This is the sequence of events...

These are the settings in my web.config file...

<facebookSettings
appId="{app id}"
appSecret="{app secret}"
canvasPage="https://apps.facebook.com/myapp/"
canvasUrl="http://www.mydomain.com/FacebookCanvas"
  secureCanvasUrl="https://www.mydomain.com/FacebookCanvas"   />


<configSections>
<section type="Facebook.FacebookConfigurationSection, Facebook" name="facebookSettings" allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<system.web>
  <httpHandlers>
      <add verb="*" path="facebookredirect.axd" type="Facebook.Web.FacebookAppRedirectHttpHandler, Facebook.Web" />
    </httpHandlers>
</system.web>

<system.webServer>
<handlers>
      <add name="facebookredirect" path="facebookredirect.axd" verb="*" type="Facebook.Web.FacebookAppRedirectHttpHandler, Facebook.Web" />
    </handlers>   
</system.webServer>

My Facebook app settings are as follows

Thanks in advance

1
How do you redirect to apps.facebook.com/myapp/myapp? What params do you pass when you call facebook.com/dialog/oauth ? - ysrb
Sorry, slight mistake there, its actually apps.facebook.com/myapp/FacebookCanvas/...In terms of params do you mean the permissions that I require? With the C# sdk, most of the heavy lifting is abstracted away, so I just specify the attribute CanvasAuthorise before the ActionResult in the controller and it handles the rest. - Barry

1 Answers

1
votes

I would first call:

public virtual ActionResult GetAccessToken()
{
            var redirect_uri = GetFacebookCallbackUrl();
            var strUrl = "https://www.facebook.com/dialog/oauth?client_id=" + Server.UrlEncode(Facebook.FacebookApplication.Current.AppId) +
                                   "&redirect_uri=" + Server.UrlEncode(redirect_uri) + "&scope=user_about_me";
            ViewBag.RedirectUrl = strUrl;
            return View(MVC.Home.Views.FBRedirect);
}

And in the fb call back:

public virtual ActionResult FbCallback()
        {
            var code = Request.QueryString["code"];

            if (!string.IsNullOrEmpty(code))
            {

                string query = "client_id=" + Facebook.FacebookApplication.Current.AppId + "&client_secret=" +
                    Facebook.FacebookApplication.Current.AppSecret + "&redirect_uri=" + Server.UrlEncode(GetFacebookCallbackUrl()) +
                    "&code=" + code;

                var response = WebRequestHelper.GetWebRequest("https://graph.facebook.com/oauth/access_token?" + query);
                //Process response here.
            }

            else
            {
                var errorReason = Request.QueryString["error_reason"];
                var error = Request.QueryString["error"];

                ContentResult res = new ContentResult();
                res.Content = "code query string does not present. reason: " + errorReason;
                res.ContentType = "text/plain";
                return res;
            }
        }

Hope this helps