【问题标题】:Google API will not recognize */127.0.0.1:*Google API 无法识别 */127.0.0.1:*
【发布时间】:2018-01-16 09:37:43
【问题描述】:

我的应用成功地集成了 Google Maps JS API/Google Places API Web Service 以创建一些Autocomplete 下拉菜单。我设置了我的 google api 浏览器密钥,以便开发(本地主机)、登台和生产 url 工作。但是,在使用此服务的任何页面上,验收测试(使用 127.0.0.1)都会中断。例如:

Capybara::Poltergeist::JavascriptError:
   One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).

   Google Maps API error: RefererNotAllowedMapError https://developers.google.com/maps/documentation/javascript/error-messages#referer-not-allowed-map-error
   Your site URL to be authorized: http://127.0.0.1:52724/clients/3
   Google Maps API error: RefererNotAllowedMapError https://developers.google.com/maps/documentation/javascript/error-messages#referer-not-allowed-map-error
   Your site URL to be authorized: http://127.0.0.1:52724/clients/3
       at https://maps.googleapis.com/maps/api/js?v=3.exp&key=(my_browser_key)&signed_in=true&libraries=places:34 in hb

我尝试将不同的 127.0.0.1 配置添加到我的浏览器密钥凭据中,但没有成功,例如: http://127.0.0.1:* , */127* , and */127.0.0.1:\d\d\d\d\d/*

最后一个看起来很有趣,因为我的测试套件的每次新运行都会在 127.0.0.1: 之后生成 5 个随机数字,如上面的错误所示。

*我不想通过更改错误中提到的 poltergeist 配置来忽略 JS 错误。话虽如此,我实际上并没有在任何这些验收测试中使用该服务。我不想测试 google 功能,我想测试围绕这些下拉菜单的自定义功能。

【问题讨论】:

  • 这个应用在前端使用 Angular 1,在后端使用 Rails 4。如果由于某种原因其中任何一个是相关的。

标签: google-maps google-maps-api-3 capybara poltergeist


【解决方案1】:

能够通过将 app_host 和 server_port 直接设置到我的 Capybara 配置中来绕过这个问题。感谢接受的答案here。我将此添加到我的 spec_helper 文件中:

 def set_host(host)
   default_url_options[:host] = host
   Capybara.app_host = "http://" + host
 end

 RSpec.configure do |config|
   config.before(:each) do
     Capybara.current_driver = :poltergeist
     Capybara.javascript_driver = :poltergeist
     set_host("127.0.0.1:30111")
     Capybara.server_port = 30111
   end
 end

然后我在我的 Google API 密钥凭据中指定了这个确切的 server_port。验收测试现已通过。

更新:

Google 的说明似乎已经过时且不完整(叹气)。经过更多实验后,只需 127.0.0.1 而不使用任何 */ 即可。

上述解决方案也有效。

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2018-08-29
    • 2012-08-03
    • 1970-01-01
    • 2018-09-02
    • 2012-11-17
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多