使用 Facebook/Google API 进行本地开发
我正在致力于将omniauth 实施到Rails 项目中。我的问题是身份验证提供商 - Twitter、Google、Facebook 等都要求我创建一个带有 url 的应用程序,该应用程序限制来自该 url 以外任何地方的身份验证请求。我需要能够在本地测试,但也需要在生产中运行代码,但例如 Facebook 不允许 2 个域,并且无论如何也不允许 localhost。
那么我有什么选择呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在工作中,我们为不同的环境设置了多个应用程序。在本地,在
/etc/hosts
中添加一个条目(假设您使用的是 Linux)例如:127.0.0.1 mydomain.local
。在本地环境的 facebook 应用程序设置中,将其添加为 url。大多数事情,除了 Facebook 需要抓取你的网站(比如按钮)的地方。
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.
这是我的解决方案的博客文章: http://make.bettermistak.es/2012/05/03/how-to-create-a-local-sandbox-facebook-app/
以下是相关内容:“Facebook 验证您的应用程序的所有请求都来自正确的域 - 它们不允许来自 localhost 或 127.0.0.1 的请求 - 并且可以在托管 URL 下的应用程序设置中更新此信息。托管 URL 并保存此设置,然后编辑 /etc/hosts 文件,使本地内容位于 local.herokuapp.com 域下,因此从命令行输入 sudo vi /etc/hosts。 (用您最喜欢的编辑器替换 vi。)我们需要使用 sudo,因为该文件已锁定。在“127.0.0.1 localhost”下面添加行“127.0.0.1 local.herokuapp.com”,然后保存并退出文本编辑器。
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."
据我所知,你必须为你的应用程序注册两个应用程序。(一个用于远程端,一个用于本地端)
幸运的是,有一种方法可以减少复杂性(假设你正在Linux上工作):
你可以配置你.bash_profile(分别是本地计算机和远程计算机):
并在中使用 ENV['YOURAPP_APP_ID'] 和 ENV['YOURAPP_APP_SECRET']你的代码。
例如,您可以在rails中进行如下编码:
通过这种方式,您可以在本地和远程端使用相同的代码。维护起来会容易很多。
如果您使用 Heroku 托管您的应用程序,您可以参考此页面来配置环境变量。
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):
And use ENV['YOURAPP_APP_ID'] and ENV['YOURAPP_APP_SECRET'] in your code.
For example, you can code like this in rails:
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.
我在 Facebook 上创建了两个应用程序,其中一个出于开发目的在沙盒模式下运行。这对你来说是一个选择吗?
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?