get_map not passing the API key (HTTP status was '403 Forbidden')
Asked Answered
E

4

11

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'
Enchain answered 29/9, 2018 at 5:40 Comment(1)
Thanks @SymbolixAU. I will check this out subsequently. For now, my problem got resolved by using get_googlemap instead of get_map. Seems get_map is not designed to accept the api_key when the source is google. get_map uses the api_key only when the source is cloudmade.Enchain
B
17

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

Bounteous answered 2/10, 2018 at 23:55 Comment(1)
Hyy, I get the following error for ggmap(get_googlemap()) "Source : maps.googleapis.com/maps/api/… Error in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array In addition: Warning message: In get_googlemap() : HTTP 400 Bad Request" .. What do I do?Babarababassu
G
6

Just to add to Roman Abashin's answer (I can't comment, unfortunately): As per '?get_map()', the 'api_key =' argument doesn't work for Google maps. You'll need to use the 'register_google()' function, but as of 03/10/18, it's only in the development version of ggmap, which you can get like so:

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

Then you'll also need to enable billing on your Google account, though the first 100,000 maps you use each month should be free, see here: https://cloud.google.com/maps-platform/pricing/sheet/ for details.

(tips drawn from here: https://github.com/dkahle/ggmap/issues/51)

Gleanings answered 3/10, 2018 at 9:21 Comment(2)
Absolutely right, thank you! Here is my attempt at a rough step-by-step tutorial.Bounteous
@ErnestScribbler I expanded my answer to also include the tutorialBounteous
L
4

I don't know the direct resolution to the ggmap issue, but if you're happy to work with an interactive map rather than a static one you can use my googelway library

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)

enter image description here

Labana answered 29/9, 2018 at 5:52 Comment(4)
I started to have the same issue as before. Nothing shows up in the R viewer and this time even opening it in a browser doesn't help. Just wanted to let you know. Cheers for all the help that I got from your packages throughout these years.Catenary
@Masoud - is there an error in the javascript console ( in the browser, right click (outside the map), select 'inspect', and go to the 'console' tab)Labana
Don't have my laptop with me, will check and report back.Catenary
@Masoud odd that was required, but glad it's now working againLabana
L
2

Just adding to @Roman's response, here's the code that worked for me:

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

For more information you could refer to the library page on github: here
Hopefully it helps!

Leech answered 17/10, 2018 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.