ggmap icon indicating copy to clipboard operation
ggmap copied to clipboard

Request Denied in geocode with API key

Open fagnersutel opened this issue 6 years ago • 14 comments

Reproducible Example

When I run the code behind below get the response ""error_message" : "Requests to this API must be over SSL. Load the API with "https://" instead of "http://"."," how to change the http request to https in geocode of ggmap servico.xlsx

Example:

setwd("/Users/fsmoura/Desktop/docs/")
library(ggmap)
       
#Carga de dados
origAddress <- read.csv("servico/servico.csv", header = TRUE, sep = ";")
#Verificamos o tamanho do data set
dim(origAddress)
#View(origAddress)
#Removemos os duplicados
origAddress <- origAddress[!duplicated(origAddress$Empresa), ]
#Verificamos o tamanho do data set
dim(origAddress)
#criamos a variavel de pesquisa
origAddress$addresses <- origAddress$Endereco
#agregamosao endereço o bairro e cidade
origAddress$addresses <- paste(origAddress$Endereco,  origAddress$Bairro, "Porto Alegre", sep = " ")
origAddress$addresses <- paste(origAddress$addresses, "&key=MYKEY", sep = "")
#Eliminamos a virgula
origAddress$addresses <- gsub(",", "", origAddress$addresses)
#fazemos o tipecast
origAddress$addresses <- as.character(origAddress$geoAddress)
#verificamos o resultado final
head(origAddress)
#definimoso range da pesquisa
origAddress<- origAddress[1:50000,]
#fazemos a restricao
addresses <- origAddress$addresses
#verificamos o tamanho
length(addresses)
#executamosa funcao
getGeoDetails <- function(address){
  geo_reply = geocode(address, output='all', messaging=TRUE, override_limit=TRUE)
  answer <- data.frame(lat=NA, long=NA, accuracy=NA, formatted_address=NA, address_type=NA, status=NA)
  answer$status <- geo_reply$status
  
  while(geo_reply$status == "OVER_QUERY_LIMIT"){
    print("OVER QUERY LIMIT - Pausando as:") 
    time <- Sys.time()
    print(as.character(time))
    Sys.sleep(1)
    geo_reply = geocode(address, output='all', messaging=TRUE, override_limit=TRUE)
    answer$status <- geo_reply$status
  }
  
  if (geo_reply$status != "OK"){
    return(answer)
  }
  answer$lat <- geo_reply$results[[1]]$geometry$location$lat
  answer$long <- geo_reply$results[[1]]$geometry$location$lng   
  if (length(geo_reply$results[[1]]$types) > 0){
    answer$accuracy <- geo_reply$results[[1]]$types[[1]]
  }
  answer$address_type <- paste(geo_reply$results[[1]]$types, collapse=',')
  answer$formatted_address <- geo_reply$results[[1]]$formatted_address
  
  return(answer)
}
geocoded <- data.frame()

startindex <- 1

for (ii in seq(startindex, length(addresses))){
  print(paste("Trabalhando no item ", ii, " de", length(addresses)))

  result = getGeoDetails(addresses[ii]) 
  print(result$status)
  result$index <- ii

  geocoded <- rbind(geocoded, result)

}
#write.csv(geocoded, "saida2.csv", row.names=FALSE)
geocoded$accuracy <- NULL
geocoded$address_type<-NULL
geocoded$accuracy<-NULL
geocoded$status<-NULL
geocoded$index<-NULL
write.table(geocoded,file="data.csv",sep=";",dec = " ", row.names=FALSE)




}, si = TRUE)

fagnersutel avatar Apr 13 '18 11:04 fagnersutel

~~That's an interesting approach to API key support. I think we're going to have to alter the guts of geocoding to do this.~~

Could you use register_google to pass your API key instead and see if that fixes this?

scottmmjackson avatar Apr 24 '18 16:04 scottmmjackson

Dear @scottmmjackson , register_google is part of which package? I have the same problem. geocode("the white house")

Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=the%20white%20house&sensor=false

 lon lat
1  NA  NA
Warning message:
geocode failed with status OVER_QUERY_LIMIT, location = "the white house" 

sanjmeh avatar Jun 26 '18 11:06 sanjmeh

Hi,

I'm also having the same issue (ggmap 2.7.900).

For a list of about 40 addresses that I was attempting to geocode I initially got the following error for the last few (I assume because I'm on a university network?);

geocode failed with status OVER_QUERY_LIMIT, location = "XXX"

So, I created an API key following these instructions: https://www.wpgmaps.com/documentation/creating-a-google-maps-api-key/

I passed the API key to mutate_geocode, but am now get nothing back at all;

register_google(key = '12345')

QF1SUB_COUNT <- mutate_geocode(QF1SUB_COUNT,QF1SUB,output="latlon",source="google")

geocode failed with status REQUEST_DENIED, location = "XXX"

Any help would be appreciated!

indysandaradura avatar Jul 16 '18 03:07 indysandaradura

@fagnersutel @scottmmjackson @sanjmeh @indysandaradura

Those two errors mean two different things.

  1. geocode failed with status OVER_QUERY_LIMIT, location = "XXX" --> you have not registered a correct and billing-enabled Google Maps API key using register_google() (ggmap v2.7,903). Enabling billing is a specific step after (!) adding your credit card information.

  2. geocode failed with status REQUEST_DENIED, location = "XXX" --> you have enabled your Google Maps API for static map requests, but not for geocoding. To prove that try geocode("Houston", output = "all") and you will get the error

$`error_message`
[1] "This API project is not authorized to use this API."

$results
list()

$`status`
[1] "REQUEST_DENIED"

This is the same for revgeocode().

rrmn avatar Oct 13 '18 23:10 rrmn

I had this problem, too!

I read the above response and it helped me, thanks @RomanAbashin!

I found it tricky to navigate, so to spell it out for others who find this:

  1. log into https://console.cloud.google.com
  2. access the 'Navigation Menu' in the top left header
  3. hover on 'APIs and Services', then click on 'Credentials'
  4. You should see your API Key listed, click 'Edit API key' icon on the right.
  5. Enable 'Geocoding API' under 'API restrictions'.

Good luck!

jimmyjamesarnold avatar Oct 15 '18 00:10 jimmyjamesarnold

This solve the problem: if(!requireNamespace("devtools")) install.packages("devtools") devtools::install_github("dkahle/ggmap", ref = "tidyup")

fagnersutel avatar Oct 20 '18 20:10 fagnersutel

I have the same problem.

I try to query the geocode of 20 locations.

Whenever I run the query, a few of them will not work.

About 40% of locations are OK.

In about 40% of locations, I get the error status "OVER_QUERY_LIMIT" and the error message "You have exceeded your daily request quota for this API. If you did not set a custom daily request quota, verify your project has an active billing account: http://g.co/dev/maps-no-account".

In about 20% of locations, I get the error status "REQUEST DENIED" and the error message "The provided API key is expired."

If I rerun the query, a different set of locations will be successful and a different set of locations will have errors. This seems completely at random, with one exception: If I delete the API KEY and generate a new one, the results will have less errors (only about 10%) for the first query, and the number of failed locations will increase the longer I use the same API key.

I believe this is a problem on Google's end, but it still makes it impossible to use the Geocode API.

martinzuba avatar Nov 16 '18 11:11 martinzuba

@martinzuba What do you get when you write geocode("Houston", output = "all")?

rrmn avatar Jan 07 '19 18:01 rrmn

I get nothing. This used to be so easy.

geocode("Houston", output = "all")

results in

$error_message [1] "The provided API key is invalid."

$results list()

$status [1] "REQUEST_DENIED"

But I have set the google api key to allow geocoding. I am not sure what to do now.

kambanane avatar Jan 17 '19 14:01 kambanane

@kambanane

This means you entered a wrong API key (e.g., by copying it wrongly from the Console) inregister_google("...").

Try to follow this tutorial https://stackoverflow.com/a/52617929/9406040 step by step.

rrmn avatar Jan 17 '19 17:01 rrmn

In case anyone is still having issues with geocoding being denied, please see ggmap's documentation and readmes about updating the package and registering your google account here: https://github.com/dkahle/ggmap/commit/177c4eaedee4746b42c5ce4684ea2677f918507a

This solved my issues with being denied.

28sophiesworld avatar Jan 31 '19 00:01 28sophiesworld

@fagnersutel @scottmmjackson @sanjmeh @indysandaradura

Those two errors mean two different things.

  1. geocode failed with status OVER_QUERY_LIMIT, location = "XXX" --> you have not registered a correct and billing-enabled Google Maps API key using register_google() (ggmap v2.7,903). Enabling billing is a specific step after (!) adding your credit card information.
  2. geocode failed with status REQUEST_DENIED, location = "XXX" --> you have enabled your Google Maps API for static map requests, but not for geocoding. To prove that try geocode("Houston", output = "all") and you will get the error
$`error_message`
[1] "This API project is not authorized to use this API."

$results
list()

$`status`
[1] "REQUEST_DENIED"

This is the same for revgeocode().

I am getting the same error , could you please help me resolve the issue.

Dinkarkumar avatar May 29 '20 00:05 Dinkarkumar

thanks for the tip:

geocode("Nevada") Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nevada&key=xxx-mx21RVnjAMXrRc0xHTQg4TuMZHAjxFI Warning: Geocoding "Nevada" failed with error: You must enable Billing on the Google Cloud Project at https://console.cloud.google.com/project/_/billing/enable Learn more at https://developers.google.com/maps/gmp-get-started

ahcyip avatar Jun 11 '20 11:06 ahcyip

When I execute revgeocode(c(lon = -97.1161, lat = 31.55098)), I got:

Warning: Reverse geocoding failed with error:
This API project is not authorized to use this API.

But strangely when I execute geocode("Houston", output = "all") I can retrive valid result:

$results
$results[[1]]
$results[[1]]$address_components
$results[[1]]$address_components[[1]]
$results[[1]]$address_components[[1]]$long_name
[1] "Houston"
....

Even more puzzling is that if I revgeocode(c(lon = -97.1161, lat = 31.55098), urlonly = TRUE) And paste the url into Chrome, I can get the result. I would like to ask did anyone encounter situation like this? Thank you.

Session Info:

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    
system code page: 950

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggmap_3.0.0   ggplot2_3.3.3

FrankNine avatar Mar 05 '21 16:03 FrankNine