【问题标题】:get_map not passing the API key (HTTP status was '403 Forbidden')get_map 未传递 API 密钥(HTTP 状态为“403 Forbidden”)
【发布时间】:2019-03-05 01:11:55
【问题描述】:

我在 R 的 get_map() 函数(ggmap 库)中遇到了这个问题。

我的代码在几个月内无需指定 API 密钥(source = "google")即可运行。然而,代码在几周前就停止工作了。我知道谷歌已经强制使用 API 密钥(或者他们可能在没有我用尽的 api 密钥的情况下允许一定数量的调用)。

但是,即使在指定 API 密钥(从 Google Cloud Platform 获得)之后,我的代码仍会以相同的方式运行。我什至联系了谷歌云支持,但他们说 API 密钥本身没有问题,他们能够在最后调用地图。

我怀疑 get_map() 函数在从 Google 调用地图时没有传递 api_key。任何指向解决方案的指针将不胜感激。

以下是可重现的代码(即失败)。

library(ggmap)

lat <- c(4,41)  # India lat boundaries
lon <- c(68,99) # India long boundaries
center = c(mean(lat), mean(lon))

map <- get_map(location = c(lon = mean(lon), 
                            lat = mean(lat)),
               api_key = <my api key>,
               zoom = 6,
               maptype = "terrain",
               source = "google",
               messaging = TRUE
)

以下是 R 中的错​​误消息(注意 API 密钥未通过)

trying URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
In addition: Warning message:
In download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") :
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false': HTTP status was '403 Forbidden'

【问题讨论】:

  • 感谢@SymbolixAU。我稍后会检查这个。现在,我的问题通过使用 get_googlemap 而不是 get_map 得到了解决。当来源是谷歌时,似乎 get_map 不是为了接受 api_key 。 get_map 仅在源为 cloudmade 时才使用 api_key。

标签: r google-maps google-maps-api-3 ggmap


【解决方案1】:

我不知道ggmap 问题的直接解决方案,但如果您乐于使用交互式地图而不是静态地图,您可以使用我的googelway

library(googleway)

set_key("GOOGLE_MAP_KEY")

lat <- c(4,41) #India lat boundaries
lon <- c(68,99) #India long boundaries
center = c(mean(lat), mean(lon))

google_map(location = center, zoom = 6)

【讨论】:

  • 我开始遇到和以前一样的问题。 R 查看器中没有显示任何内容,这一次即使在浏览器中打开它也无济于事。只是想让你知道。为这些年来我从您的包裹中获得的所有帮助干杯。
  • @Masoud - javascript 控制台中是否有错误(在浏览器中,右键单击(在地图外),选择“检查”,然后转到“控制台”选项卡)
  • 不要带我的笔记本电脑,我会检查并报告。
  • @Masoud 奇怪,这是必需的,但很高兴它现在又可以工作了
【解决方案2】:

您需要在 R 的每个新会话中使用 register_google(key = "...")。在 get_map() 调用中使用 api_key = 不起作用。


更新时间:2018 年 12 月 24 日,用于 ggmap 2.7.904 和当前的 Google Cloud API

分步教程

1。更新到最新版本的ggmap

require(devtools)
devtools::install_github("dkahle/ggmap", ref = "tidyup")

2。在 Google Cloud Console 中为所有 API 激活您的 Google API 密钥

3。加载ggmap并注册key

​​>
library(ggmap)
register_google(key = "...")     # copied directly from Google Console via 'copy' button

4。绘制默认地图

ggmap(get_googlemap())          

5。带有位置名称的绘图(地理编码)

ggmap(get_map("Hannover, Germany"))

如果您在此处收到错误消息(例如,Forbidden 403),您很可能没有为正确的 API 激活密钥。 Tutorial to troubleshoot geocoding

6。绘制经度和​​纬度

ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))

【讨论】:

  • 嘿,我收到ggmap(get_googlemap()) 的以下错误“来源:maps.googleapis.com/maps/api/… aperm.default(map, c(2, 1, 3)) 中的错误:第一个参数无效,必须是array 另外:警告消息:在 get_googlemap() 中:HTTP 400 Bad Request" .. 我该怎么办?
【解决方案3】:

只是为了补充 Roman Abashin 的回答(遗憾的是,我无法发表评论):根据 '?get_map()','api_key =' 参数不适用于 Google 地图。您需要使用 'register_google()' 函数,但截至 2018 年 3 月 10 日,它仅在 ggmap 的开发版本中,您可以像这样获得:

devtools::install_github("dkahle/ggmap", ref = "tidyup")

然后您还需要在您的 Google 帐户上启用计费功能,尽管您每月使用的前 100,000 张地图应该是免费的,请参阅此处:https://cloud.google.com/maps-platform/pricing/sheet/ 了解详情。

(来自这里的提示:https://github.com/dkahle/ggmap/issues/51

【讨论】:

【解决方案4】:

只是添加到@Roman 的回复中,这是对我有用的代码:

if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
register_google(key = "your_API_key") 
usa<- get_googlemap(location='united states', zoom=4,maptype = "hybrid")

更多信息可以参考github上的库页面:here
希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2020-01-11
    • 2020-06-21
    • 1970-01-01
    • 2021-12-12
    • 2020-01-27
    • 2013-03-21
    相关资源
    最近更新 更多