r - get_map 未传递 API key (HTTP 状态为 '403 Forbidden' )

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

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

我的代码运行了几个月,无需指定 API key (对于 source = "google")。然而,该代码在几周前停止工作。我了解到 Google 已强制要求 API key (或者可能他们允许在没有我用尽的 api key 的情况下进行一定数量的调用)。

但是,即使指定了 API key (从 Google Cloud Platform 获取),我的代码仍然以相同的方式运行。我什至联系了 Google Cloud 支持,但他们说 API key 本身没有任何问题,他们能够在最后调用 map 。

我怀疑 get_map() 函数在从 Google 调用 map 时未传递 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 key 没有被传递)

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'

最佳答案

您需要使用register_google(key = "..." )在 R 的每个新 session 中。使用 api_key = 里面get_map()通话无法进行。

<小时/>

已更新: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 key

enter image description here

enter image description here

3。加载ggmap并注册 key

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

4。绘制默认 map

ggmap(get_googlemap())          

Houston

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

ggmap(get_map("Hannover, Germany"))

If you get an error here (e.g., Forbidden 403) you most probably have not activated your key for the right APIs. Tutorial to troubleshoot geocoding

Hannover

6。经度和纬度绘图

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

3

关于r - get_map 未传递 API key (HTTP 状态为 '403 Forbidden' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52565472/

相关文章:

r - 将来自 Quantstrat 的订单簿写入 CSV 文件

google-maps - 使用 Google Maps API 进行地理编码 - 准确性和局限性

android - 如何在选项卡式 Activity 中的 SupportMapFragment 上设置标记?

php - 查找附近的邮政编码 myquery 优化

javascript - 如何解决 “cannot call method … of undefined” 错误?

r - 翻译函数: "error:sequence is not a vector of chars"

r - 如何获取 R 代码表达式中的所有变量?

html - Rvest 网页抓取带来仅包含列名称的空数据表

javascript - 如何在 Google map v3 中的每个标记上添加编号?

javascript - Google map API v3 - 添加多个信息窗口