使用API密钥在ggmap中进行映射时出现错误(403禁止访问)。

7

我通常使用ggmap在简单的城市地图上绘制点。今天我遇到了一个新错误,禁止我使用函数get_map()

        #get API key @ https://developers.google.com/places/web-service/get-api-key
    key<-"AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y"
    register_google(key = key)

atw<- get_map(location=c(-88.68,42.14), zoom=10, scale=2)

我不确定问题出在哪里。我尝试了一个新的API密钥,但没有成功。有任何建议吗?

错误信息如下:

无法打开网址'https://maps.googleapis.com/maps/api/staticmap?center=42.14,-88.68&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y':HTTP状态为'403 Forbidden'。在download.file(url, destfile = destfile, quiet = !messaging, mode = "wb")中出现错误:无法打开URL 'https://maps.googleapis.com/maps/api/staticmap?center=42.14,-88.68&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y'
2个回答

6

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

问题

您的API密钥

  • 要么无效(输入错误)/未启用计费(最可能的原因),或者
  • 存在一些连接/代理问题。

请查看Stackoverflow上的逐步教程

解决方案

要检查问题所在,请键入geocode("Houston", output = "all")并查看错误消息。

1. 错误的API密钥

> geocode("Houston", output = "all")
$error_message
[1] "The provided API key is invalid."

$results
list()

$status
[1] "REQUEST_DENIED"

这意味着您提供的API密钥未被谷歌识别。可能是输错或者复制错误了?有时候也会出现奇怪的问题,所以请在谷歌控制台中生成一个新的API密钥,然后再重试

2. API密钥未启用地理编码

> geocode("Houston", output = "all")
$`error_message`
[1] "This API project is not authorized to use this API."

$results
list()

$`status`
[1] "REQUEST_DENIED"

这意味着你的API密钥是有效的,但你还没有允许使用这个特定的API。请记住:谷歌为每一种小请求(静态地图、方向、地理编码等)都有一个API。因此,在这种情况下,你需要前往你的谷歌控制台并启用正确的API,比如地理编码

启用所有API的工作输出

> ggmap(get_map("Houston"))

plot


2
如果您的API密钥有效,您还可以使用library(googleway)来绘制交互式地图。
library(googleway)

## you can use separate API keys for different APIs
set_key( "GOOGLE_API_KEY", api = "geocode")
set_key( "GOOGLE_MAP_KEY", api = "map")

## you can view the keys you have with
google_keys()

google_map( location = c(52, 0), zoom = 6 )

enter image description here

## add a marker by geocoding an address
res <- google_geocode("Buckingham Palace")
loc <- geocode_coordinates( res )

google_map() %>%
  add_markers(data = loc)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接