13

the title might suggest it, but i'm looking for a way to have Facebooks graph API work on my localhost. It's a pain if I need to sync up the project to the server every time I want to test, because graph only works when online.

Does anyone have any suggestions on this problem?

Kara
  • 6,115
  • 16
  • 50
  • 57
Michiel Standaert
  • 4,096
  • 7
  • 27
  • 49
  • Not sure what you mean: do you mean implement a mock Facebook graph API or run your web app on an HTTP server on localhost and access the Facebook Graph API online? – Femi May 02 '11 at 14:44
  • I am working on my own computer and i have Xampp running my localhost. I would like to access facebook through graph api without having to upload the project to a webserver. – Michiel Standaert May 02 '11 at 14:46
  • possible duplicate of [Running facebook application on localhost](http://stackoverflow.com/questions/5133075/running-facebook-application-on-localhost) – ifaour May 02 '11 at 17:17
  • https://www.youtube.com/watch?v=-v48ONWva30 – Fábio Zangirolami Nov 20 '17 at 21:57

6 Answers6

16

If you don't need offline access (which is not available, see Jimmy Sawczuk's answer), but only need your website to be able to access the Graph API from your localhost instead of the real domain name, it should be possible.

What you need to do is edit the settings for your site's app on Facebook. Set your applications URL to either 'localhost' or your computers local IP-address, and I think it should work.

Adrian Schmidt
  • 1,886
  • 22
  • 35
  • 3
    incase anyone runs into this problem: I was trying to get it to work from a domain on my local host mydomain:8887, when I tried entering it as the URL facebook said it wasn't a valid url, to fix this I edited the virtual hosts (in httpd.conf) to change it to mydomain.com:8887 and then facebook accepted it – Sabrina Leggett Feb 12 '12 at 14:48
  • 1
    Note that you may need to change your Site URL - AND - App Domains to 'localhost' - of course, that may have changed by the time I finished writing this comment. – Adam Pearlman Oct 03 '17 at 19:43
3

tl;dr

  1. Setup the facebook app
  2. Edit your /etc/hosts file with a local.yourdomain
  3. use your browser on local.yourdomain

Follow the instructions described in this post for the how-to: How To: Local Facebook App Development

Sebastian Sastre
  • 2,034
  • 20
  • 21
1

Unless you plan on building a fake Facebook Graph API endpoint, then yes, you need to be online to query the graph API.

As for running off of localhost, you'll find that you need an endpoint to redirect users to on an install or Facebook will throw an error. You should be able to use an IP address, but a domain name is going to save you a lot of hassle.

When I'm developing, I use a testing Facebook app that points to my testing server and when I push my code live, it uses the real app which is based on the real domain. Additionally, my testing server is a VM that accesses files on my computer via a Samba share.

Hope this helps.

Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • are you certain there is no way to configure xampp or mamp in any way i could test from the localhost? (it's not actually for me, it's for someone else and he says uploading to a webserver isn't an option at this moment. He basically just wants to get the name and profile picture from a user and he wants his website running locally). – Michiel Standaert May 02 '11 at 14:49
  • btw: I know there is a way to do this in asp.net, i just don't know if it is also possible for php. this is the link to the asp.net way of doing it: http://facebooksdk.com/docs/2010/10/how-to-set-up-local-development-environment-for-facebook-application-development-using-asp-net/ – Michiel Standaert May 02 '11 at 14:50
  • Maybe I'm misunderstanding you, but the link you posted 404'ed. – Jimmy Sawczuk May 02 '11 at 14:52
  • I expanded on my answer a little bit to say how I configured my dev environment. Hope it helps. – Jimmy Sawczuk May 02 '11 at 14:57
  • well, the problem is solved now. If you set the website-url on the developers.facebook.com website in your app to your localhost, it works just fine. Thanks for the comment anyways. – Michiel Standaert May 02 '11 at 15:02
0

I am running rails 3.2.8 and this worked for me too. However on a mac with mountain lion, the easiest way that I found was to create an alias under localhost for 'somedomain.com:3000' in the etc/hosts file for the site url and with out the port # on the app domain.

jhicks4
  • 41
  • 5
0

OK. I just spent best part of a day working this out. If you don't have access to the API panel to edit what domains your facebook api allows (and you need to use SSL to fake it out).

Here's what I did:

I am running a node server on my desktop.

You'll need to make sure your node server is already pointing at whatever files you want to run/include in your project. (i.e. if you are not using node to develop, this solution is probably not going to help you).

Make sure you have express and vhost installed. I created a server key and certificate using the command line like this:

openssl genrsa -out myKey.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out myCert.pem
rm csr.pem

I moved the two files into my current node directory where I am running my server instance from.

I created the server instance like this:

var vhost = require('vhost'),
express = require('express'),

vhost: {

     'default': 'www.mybigfakeserver.com'   // this should match what your api key allows
},

require('https').createServer({
        key: fs.readFileSync('myKey.pem'),
        cert: fs.readFileSync('myCert.pem')
}, app).listen(443);

Inside your hosts file add whatever you need to fake out. e.g.

127.0.0.1 www.mybigfakeserver.com

This is taken from the facebook api. You will need to add this in your HTML file:

<script>

     FB.init({
        appId      : 'PUT YOUR APP KEY HERE',
        version    : 'v2.0',
        status: true
     });
function(d, s, id){
  var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script'
, 'facebook-jssdk'));

</script>

Fire up your node server.

Browse to https://www.mybigfakeserver.com

You should see your site. It should now be able to fake out your facebook api into thinking you are running on your regular server that the app was created for.

Go do facebook development without having to deploy all the time.

John Green
  • 51
  • 1
  • 3
0

Odd, but have you tried modifying your local DNS override (either /etc/hosts or \WINDOWS\system32\drivers\etc\hosts) to point the domain you provided to your Facebook app at localhost? That might work.

I'm betting they use some kind of referer URL check, and if they do you can visit the URL in your browser, it will route to localhost, it will hit the graph api with the right referer and you should be golden.

Let me know if it works.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Just realized: the url you pointed to does exactly what I said. – Femi May 02 '11 at 14:59
  • well, the problem is solved now. If you set the website-url on the developers.facebook.com website in your app to your localhost, it works just fine. Thanks for the comment anyways. – Michiel Standaert May 02 '11 at 15:02