Dan
Dan

Reputation: 5637

Authenticating with Facebook from local development site

I'm trying to add the ability to post to a users wall from a site I'm developing.

I've got some code in place using the Microsoft C# Facebook SDK. It checks if the current user is authenticated, if not, redirects to the OAuth page. If the user is authenticated, it tries to post to their wall:

CanvasAuthorizer auth = new CanvasAuthorizer();

bool authorized = auth.FacebookWebRequest.IsAuthorized(new string[] { "publish_stream" });

if (authorized)
{

    FacebookClient client = new FacebookClient();

    client.AccessToken = auth.FacebookWebRequest.AccessToken;

    Dictionary<string, object> args = new Dictionary<string, object>();

    args["message"] = "This is a test.";
    args["name"] = "";
    args["link"] = "http://subdomain.domain.lom/";
    args["description"] = "Development site.";

    client.Post("/me/feed", args);

}
else
{

    long appId = {APP_ID};
    string returnUrl = "http://subdomain.domain.com/share";

    Response.Redirect("https://www.facebook.com/dialog/oauth?client_id=" + appId.ToString() + "&redirect_uri=" + returnUrl + "&scope=publish_stream");

}

The first time you load, it sees that you're not authenticated and redirects you to the Facebook auth page.

However, when it tries to return to my site, it goes into a redirect loop.

It looks like what is happening is that when it returns from Facebook, it does the authentication check, but it's saying the users isn't authenticated. This then redirects to the Facebook login again, which knows the user is authenticated, so returns to my site, causing the loop.

I'm developing on my local machine at the moment, http://subdomain.domain.lom/, which isn't accessible from the outside world.

My Facebook App settings are:

I was under the impression that it doesn't matter if you're working locally?

Upvotes: 3

Views: 298

Answers (2)

Grzegorz Gierlik
Grzegorz Gierlik

Reputation: 11232

Sandbox mode

I would suggest creating second Facebook application (in terms of Facebook app id) for development and testing with sandbox mode turned.

With the testing app you can use the same code with different settings on limited number of users hidden from anyone else (really useful).

Here are some links:

  1. Application Security from Facebook docs.
  2. How do you limit a Facebook app to a small number of people during testing? from stackoverflow.com.

Possible bugs

Try encode returnUrl value.

It it won't work you have to debug it :(. My first guest is Facebook authorized you for another domain.

Upvotes: 1

DMCS
DMCS

Reputation: 31870

Something else to look at would be for IE to work without that infinite redirect looping, it needs to see a p3p policy from your web server.

Check in your web.config for the p3p header. If it's not there, then you can add one to make IE happy.

See http://www.hanselman.com/blog/TheImportanceOfP3PAndACompactPrivacyPolicy.aspx

Upvotes: 0

Related Questions