开发者

How can I make Facebook's graph API work on my localhost?

the title might suggest it, but i'm looking for a way to have Facebooks graph API work on my localhost. It'开发者_JAVA百科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?


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.


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


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜