get_map未传递API密钥(HTTP状态为"403禁止") [英] get_map not passing the API key (HTTP status was '403 Forbidden')

查看:111
本文介绍了get_map未传递API密钥(HTTP状态为"403禁止")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的代码运行了几个月,而无需指定API密钥(对于source = "google").但是,该代码在几周后停止工作.我了解到Google已将API密钥设为强制性的(或者在没有我用尽的api密钥的情况下,他们可能允许一定数量的调用).

但是,即使指定了API密钥(从Google Cloud Platform获取),我的代码仍然继续以相同的方式运行.我什至联系了Google Cloud Support,但是他们说API密钥本身没有什么问题,他们可以在最后调用地图.

我怀疑从Google调用地图时get_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密钥)

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'

解决方案

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


更新:ggmap 2.7.904和当前的Google Cloud API的2018年12月24日

分步教程

1.更新到最新版本的ggmap

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

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

3.加载ggmap并注册密钥

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

4.绘制默认地图

ggmap(get_googlemap())          

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

ggmap(get_map("Hannover, Germany"))

如果您在此处遇到错误(例如,禁止访问403),则很可能您尚未为正确的API激活密钥. 用于对地址解析进行故障排除的教程

6.绘制经度和​​纬度

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

I have been facing this issue in the get_map() function (ggmap library) in R.

My code was running without the need to specify an API key (for source = "google") for several months. However, the code stopped working a couple of weeks back. I understood that Google has made the API key mandatory (or maybe they allowed a certain no of calls without the api key which I exhausted).

However, even after specifying the API key (obtained from Google Cloud Platform) my code continued behaving the same way. I even contacted Google Cloud Support, but they said there was nothing wrong with the API key per se and they were able to invoke the map at their end.

I suspect the get_map() function is not passing the api_key while invoking the map from Google. Any pointers towards resolution would be appreciated.

Below is the reproducible code (that is failing).

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
)

And below is the error message in R (note the API key is not getting passed)

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'

解决方案

You need to use register_google(key = "...") in every new session of R. Using api_key = inside the get_map() call does not work.


updated: 2018-12-24 for ggmap 2.7.904 and current Google Cloud API

Step-by-Step Tutorial

1. Update to newest version of ggmap

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

2. Activate your Google API key for all APIs in the Google Cloud Console

3. Load ggmap and register key

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

4. Plot default map

ggmap(get_googlemap())          

5. Plot with location name (Geocoding)

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

6. Plot with longitude and latitude

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

这篇关于get_map未传递API密钥(HTTP状态为"403禁止")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆