Facebook login - how to develop on both localhost

2020-02-24 10:47发布

问题:

I am developing a website which uses the Facebook login. Now, I want to be able to get this to work both in my production environment, as well as in my development environment.

On Facebook, I can give ONE site url, which Facebook can redirect to. This worked great during my development phase, but now I want it both to work in production, but also while developing my solution.

I guess one way would be to run my application on the local IIS with my domain-navn and change my hosts file, but that can't seriously be the most easy solution!?

- What is the easiest way to get it to work in both environments?

回答1:

Update: As of 2018 the path to this setting is now Products > Facebook Login > Settings > Client OAuth Settings the rest of this answer is still valid.


There is a better way. You just need to add valid callback URL's for your localhost to Settings > Advanced > OAuth Settings.

This method allows you still have your production website URL while allowing oauth from dev, staging, production, etc.



回答2:

After I have worked with this for ~6 months, I have found a solution which works really well, based on the other two answers.

In Facebook I add two apps:

  • Production app
  • Dev app

Then, in my web.config app settings I have:

<!--Prod-->
<add key="_FacebookApiId" value="id"/>
<add key="_FacebookApiSecretId" value="secret_id"/>
<!--Dev-->
<add key="FacebookApiId" value="dev_id"/>
<add key="FacebookApiSecretId" value="dev_secret_id"/>

Which of course is read through a helper-class.

Then it works seamlessly on both production and localhost, and both myself and my team is happy.



回答3:

I have the same problem.

The only solutions I have found are:

  • yes... edit the host file on your local machine: How to Test Facebook Connect Locally
  • create another Facebook app only for development purposes: Facebook Oauth on localhost for testing during development (the blog is about Rails but the solution to use different apps depending on the environment is still valid for other languages)


回答4:

Following the advice in this answer, it will work if set the Mobile URL to be your development domain (ie. localhost) and the Site URL to be your production domain.



回答5:

The updated way.

In the App Dashboard, choose your app and go to Products > Facebook Login > Settings. Under the Client OAuth Settings, enter your redirect URL in the Valid OAuth redirect URIs field for successful authorization.

Go to https://developers.facebook.com/docs/facebook-login/web for more info.



回答6:

Facebook only allows localhost OAuth redirects when your app is in development mode. This means you can't run your application in production and perform local development tests.

Instead, you need to create a test variant of your app. Head to https://developers.facebook.com and view the dashboard for your production application. Click the drop-down menu at the top-left and choose Create Test App.

This populates a test application with almost the same settings as your production app. However, it gives you a new app ID and app secret, so you'll need to toggle these settings when running in development mode (as other answers suggest).

Here's some further info on test apps: https://developers.facebook.com/docs/apps/test-apps/