【问题标题】:Cannot connect to google directions API无法连接到谷歌路线 API
【发布时间】:2016-06-13 06:38:53
【问题描述】:

我正在尝试通过 Android 应用程序使用 Google Directions API。

在 google 开发者控制台上,我创建了一个项目并选择了 Android 平台。

使用我唯一的包名称和 SHA1 密钥,我得到了一个 API 密钥。

在我的 android 代码中,我使用此 API 密钥发送 HTTP 请求。

url = "https://maps.googleapis.com/maps/api/directions/json?origin=Dhanmondi&destination="+destination+"&key=" + apiKey;



public String sendGetRequest(String uri) {
    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String result;

        StringBuilder sb = new StringBuilder();

        while((result = bufferedReader.readLine())!=null){
            sb.append(result);
        }

        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

但是,我收到了这个错误。

此 IP、站点或移动应用程序无权使用此 API 密钥。从 IP 地址 112.13.34.221 收到的请求,引用者为空。

我已启用 Google Maps for Android API 以及 Google Directions、Google Roads 和 Google Places API

【问题讨论】:

  • 您是否在创建密钥的过程中添加了相同的包名,是否启用了路线 API,并在清单中添加了密钥
  • 我有你提到的一切@war_Hero
  • 返回的错误状态码是什么,如果您将 url 粘贴到浏览器选项卡中,请检查是否收到响应
  • 状态码是 403 ...我已经编辑了我的问题,提供了屏幕截图 ... URL 在浏览器中不起作用,因为我在创建项目时选择了 android 平台@war_Hero
  • 跟url解析有关

标签: android google-maps google-api google-directions-api


【解决方案1】:

确保您在创建 api 密钥时提供了正确的包名称。

您是否创建了 android 密钥或服务器密钥或浏览器密钥。?确保你已经创建了 android key 。 方向api需要android key。

确保您已在 google 控制台中为您的 api 密钥启用了方向 api。

在创建 api 时记住所有这些要点,让我们知道接下来会发生什么。

谢谢

【讨论】:

  • 我按照你描述的所有步骤,但仍然是同样的错误@Sagar Nayak
【解决方案2】:

当您未在 Google 控制台上启用 Api 时,通常会出现此类错误。因此,请在控制台上启用您的 Api。 (Google API Key issues with Google Places)

如果您的请求格式错误或提供的签名无效,Google Maps Directions API 会返回 HTTP 403(禁止访问)错误。 (Troubleshooting authentication issues)

【讨论】:

    猜你喜欢
    • 2013-07-30
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2020-04-02
    相关资源
    最近更新 更多