Andrew Kaplan
Andrew Kaplan

Reputation: 31

Facebook C# SDK Authorization Problem

I have an iFrame Facebook application. I am using the Facebook C# SDK, Facebook and Facebook.Web libraries.

When a user first comes to my application I create a FacebookApp object on page_load(). If the app.Session is null I create a CavasAuthorizer(app) then call Authorize().

Two problems:

First, the redirect URL that is generated by calling Authorize causes Facebook to error with a bad "next" parameter. Says the "next" parameter is not owned by the application. It looks like this:

next=http://localhost:4002/facebookredirect.axd/mygame_dev/mygame.aspx

I can edit the code in CanvasURLBuilder to make the next look like this:

next=http://localhost:4002/mygame.aspx

At this point the URL works if I cut and paste into a browser however it brings me to my second issue.

When the code runs the user is presented with a mostly empty page with a mid-sized Facebook image and a link "go to Facebook". When the user clicks on the link it then takes the user to the correct authorization page for my application.

I have a feeling these are two possibly related issues but potentially separate.

Any help here would be greatly appreciated.

-Andy

Upvotes: 3

Views: 6982

Answers (3)

Carlos Muñoz
Carlos Muñoz

Reputation: 17804

For the first problem

Make sure the site Url in the app configuration page is set to http://apps.facebook.com/[your_app]/

alt text

For the second problem.

When you are not authorized you are redirected to the login url but you can't do it from your iframe since it will redirect the iframe and you will get a Facebook inside facebook. You should use window.top.location = ... to redirect the parent window.

EDIT

Facebook C# SDK Already does this for you when using the Mvc part of the SDK. Since you are using webforms you should use this code that is the equivalent.

protected void Page_Load(object sender, EventArgs e)
{
    var fb = new FacebookApp();
    var auth = new CanvasAuthorizer(fb);

    if (!auth.IsAuthorized())
    {
        var url = auth.GetLoginUrl(new HttpRequestWrapper(Request));

        var content = CanvasUrlBuilder.GetCanvasRedirectHtml(url);
        Response.ContentType = "text/html";
        Response.Write(content);
        Response.End();
        return;
    }

    //...Go on if authorized
}

Upvotes: 2

Odisea
Odisea

Reputation: 1

To test locally make sure your Site URL and Canvas URL in facebook is something like http://localhost:8181/yourpage. Then make sure in VS you set the setting under project properties\Web so that ports are not dynamically generated and you can use 8181 (or whatever port you like, as long as it is the same as in FB).

Upvotes: 0

Soshimo
Soshimo

Reputation: 171

I think your Callback url in your web config is pointing to localhost. You can't do that since the code actually runs inside of Facebooks IFrame. Localhost is localhost to the facebook web server. You will have to give a valid url in the Callback key in your web.config. If you don't have a domain you can map to your ip then check out any of the free dynamic dns clients out there. No-ip is one exammple (and one I use personally).

Upvotes: -1

Related Questions