Make local development work with Facebook/Google APIs

谁都会走 提交于 2019-12-10 11:48:55

问题


I'm working on implementing omniauth into a Rails project. My problem is that the authentication providers - Twitter, Google, Facebook etc all require me to create an application with a url that limits authentication requests from anywhere other than the url. I need to be able to to test locally but also run code in production, but Facebook for example doesn't allow 2 domains and doesn't allow localhost anyhow.

So what are my options?


回答1:


At work, we have multiple applications setup for the different environments. On local, add an entry in /etc/hosts (assuming you are on linux) eg: 127.0.0.1 mydomain.local.

On the facebook app setup for the local environment, add this as the url. Most things, except where facebook needs to scrape your site (Like buttons) work.




回答2:


Here's a blog post with my solution: http://make.bettermistak.es/2012/05/03/how-to-create-a-local-sandbox-facebook-app/

Here's the relevant bit: "Facebook verifies that all requests for your app are coming from the right domain–they don’t allow requests from localhost or 127.0.0.1–and this info can be updated in your apps settings under Hosting URL. Add “local.herokuapp.com” to your Hosting URLs and save this setting. Then edit your /etc/hosts file so that local content is under the domain local.herokuapp.com. This file is hidden, so from the command line enter sudo vi /etc/hosts. (Substitute your favorite editor for vi.) We need to use sudo, because this file is locked. Add the line “127.0.0.1 local.herokuapp.com” below “127.0.0.1 localhost” and save and quit your text editor."




回答3:


As far as I know, you must sing up two apps for you app.(one for remote side, one for local side)

Fortunately, there is a way to reduce the complication(Assuming you are working on linux):

You can configure you .bash_profile (local machine and remote machine separately):

export YOURAPP_APP_ID="XXXXX"                                         
export YOURAPP_APP_SECRET="XXXXX"

And use ENV['YOURAPP_APP_ID'] and ENV['YOURAPP_APP_SECRET'] in your code. For example, you can code like this in rails:

config.omniauth :facebook, ENV['YOURAPP_APP_ID'], ENV['YOURAPP_APP_SECRET']

By this way, you can use the same code in both local and remote side. It will be much easier for maintaining.

If you are using Heroku to host your application, you can refer to this page to config the environment variables.




回答4:


I have created two apps on Facebook one of which i run in sandbox mode for development purposes. Would that be an option for you?



来源:https://stackoverflow.com/questions/8818977/make-local-development-work-with-facebook-google-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!