Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

r - get_map not passing the API key (HTTP status was '403 Forbidden')

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

enter image description here

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())          

Houston

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

Hannover

6. Plot with longitude and latitude

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

3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...