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

问题描述

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

我的代码运行了几个月,无需指定 API 密钥(对于 source = "google").但是,该代码在几周前停止工作.我知道谷歌已经强制使用 API 密钥(或者他们可能允许在没有我用尽的 api 密钥的情况下进行一定数量的调用).

但是,即使指定了 API 密钥(从 Google Cloud Platform 获得),我的代码仍然以相同的方式运行.我什至联系了谷歌云支持,但他们说 API 密钥本身没有问题,他们能够在最后调用地图.

我怀疑 get_map() 函数在从 Google 调用地图时没有传递 api_key.任何指向解决方案的指针将不胜感激.

以下是可重现的代码(即失败).

库(ggmap)lat <- c(4,41) # 印度纬度边界lon <- c(68,99) # 印度长边界中心 = c(平均值(纬度),平均值(经度))地图 <- get_map(location = c(lon = mean(lon),纬度=平均值(纬度)),api_key = <我的 api 密钥>,缩放 = 6,maptype = "地形",来源 = "谷歌",消息传递 = 真)

下面是 R 中的错​​误信息(注意 API 密钥没有通过)

尝试 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'download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") 中的错误:无法打开 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=假'另外: 警告信息:在 download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") 中:无法打开 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 状态为 '403 Forbidden'
解决方案

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

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

分步教程

1.更新到最新版本的 ggmap

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

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

  • 3.加载ggmap并注册key

    库(ggmap)register_google(key = "...") # 通过复制"按钮直接从 Google 控制台复制

    4.绘制默认地图

    ggmap(get_googlemap())

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

    ggmap(get_map(德国汉诺威"))

    如果您在此处收到错误消息(例如 Forbidden 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"))
    

    6. Plot with longitude and latitude

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

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

08-29 05:04