【问题标题】:Google maps RefererNotAllowedMapError in Electron application谷歌在电子应用程序中映射RefererNotAllowedMapError
【发布时间】:2018-08-31 10:58:17
【问题描述】:

当使用包含 Electron 应用程序的谷歌地图应用程序时,我面临来自谷歌的 RefererNotAllowedMapError 错误,这意味着当前应用程序 url 无法使用当前 APIKey 对谷歌地图进行请求。 从理论上讲,我们可以将应用程序域列入白名单或在 google 面板中设置通配符(我想省略)。

我尝试了多种方法:

  • 在 google 地图面板中允许来自 file:/// 的请求,但 google 说“file:///”不正确。问题是电子中的路径有 3 个斜线
  • 使用插件设置推荐人 (https://github.com/akameco/electron-referer) - 运气不好
  • 使用设置引荐来源网址

    Object.defineProperty(document, "referrer", {get : function(){ return "mydomain.com"; }});
    
  • 在 loadUrl 中设置引荐来源 - 运气不好

    mainWindow.loadURL(url.format({
        pathname: 'index.html',
        protocol: 'file:',
        slashes: true
    }), {
        httpReferrer: 'mydomain.com',
    })
    

还有其他方法可以解决这个问题吗?

【问题讨论】:

  • 运行应用程序时window.location.href 的确切值是多少?
  • @xomena 这是文件:///web/#/

标签: google-maps google-maps-api-3 electron


【解决方案1】:

根据 Google Maps JavaScript API 文档:

file:// 引用者需要一个特殊的表示来添加到 Key 限制中。在添加到 Key 限制之前,应将“file:/”部分替换为“__file_url__”。例如,“file:///path/to/”应格式化为“__file_url__//path/to/*”。启用 file://referers 后,建议您定期检查您的使用情况,以确保它符合您的期望。

来源:https://developers.google.com/maps/documentation/javascript/get-api-key#key-restrictions

因此,对于 file:///web/#/ 路径,您应该将 HTTP 引荐来源限制定义为

__file_url__//web/*

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2021-02-14
    相关资源
    最近更新 更多