Error when mapping in ggmap with API key (403 Forbidden)
Asked Answered
A

2

7

I normally use ggmap to plot points on a simple city map. When doing this today I came up on a new error that forbids me to use the function 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)

I am not sure where the problem is. Ive tried a new API key but no luck. Any input?

The error reads:

cannot open 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': HTTP status was '403 Forbidden'Error in download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") : cannot open 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'

Albano answered 23/7, 2018 at 15:2 Comment(0)
L
6

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

Problem

Your API key is

  • either not valid (wrongly typed) / not enabled for billing (most probable cause) or
  • there are some connection/proxy issues.

Check out this step-by-step tutorial on Stackoverflow.

Solution

To check what the issue is type geocode("Houston", output = "all") and look at the error message.

1. Wrong API key

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

$results
list()

$status
[1] "REQUEST_DENIED"

This means you have provided an API key that is not recognized by Google. Maybe mistyped, maybe miscopied? Sometimes there are weird issues, so generate a new API key in the Google Console and try again.

2. API key not enabled for geocoding

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

$results
list()

$`status`
[1] "REQUEST_DENIED"

This means your API key is valid, but you have not allowed usage of this specific API. Remember: Google has an API for every little type of request (static maps, directions, geocoding, ...). Therefore, you need to go to your Google Console and enable this API key for the right APIs, in this case Geocoding.

Working output with all APIs enabled

> ggmap(get_map("Houston"))

plot

Lemcke answered 2/10, 2018 at 22:24 Comment(0)
C
2

If you're API key is working you can also use library(googleway) to plot interactive maps

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

Collative answered 8/12, 2018 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.