Running Facebook authentication on localhost
It has sure taken its sweet time, but I have finally started working with an ASP.NET MVC3 project that will use the Facebook API to create and login users. It’s really easy, and quite cool, and I curse myself for not having a look at this earlier.
However, after creating a Facebook app and pasting in the short code snippet that lets you log in with your Facebook account, I noticed that I was not allowed to do so from my localhost:
API Error Code: 191 API Error Description: The specified URL is not owned by the application Error Message: redirect_uri is not owned by the application.
Sure, you must enter an app URL when you register your app, but I really expected it to be possible to use the API while developing locally…
…and it turned out that it is. This is how you do it:
- Create a web site, if you do not have one already
- Create the FB app that will be used by the web site
- Give the FB app an URL that you can manage, e.g. http://myapp.mydomain.com/
- Make sure that the URL exists – it can be an empty folder, but it must exist and be public
- If you upload some of the FB code snippets to this URL, they should work.
To enable the FB features on localhost, you must create a web site with the same binding as the URL above. I have only tried this with IIS 7 / Windows 7 – please let me know if you make it work on Apache, in OS X etc.
- Setup a new web site in IIS, with the binding http://myapp.mydomain.com/
- Add 127.0.0.0 http://myapp.mydomain.com/ to your hosts file
- Now, navigate to the local web site by entering http://myapp.mydomain.com/
- If you add some of the FB code snippets to this local site, they should work.
That’s it. Note, however, that some FB features will not work if the live site misses certain pages. For instance, if you use the Like button to like a local page, it will not work if the URL does not exist at the live site.