【问题标题】:Not getting Places data using google Places API - This API project is not authorized to use this API未使用 google Places API 获取 Places 数据 - 此 API 项目无权使用此 API
【发布时间】:2015-07-28 05:53:12
【问题描述】:

我正在使用此处提出的问题中的代码Autocomplete textview google places api description -> place_id。我更改了代码以将其添加到我的应用程序中。当我运行代码时,它会在我运行代码时给出以下响应

07-28 11:17:19.244: E/result(350): {"error_message":"This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https:\/\/code.google.com\/apis\/console","predictions":[],"status":"REQUEST_DENIED"}

我搜索了一些线程并从以下线程中搜索

  1. Maps API keys for geocoding not working
  2. This IP, site or mobile application is not authorized to use this API key 和其他一两个

我按照建议执行了以下步骤。

  1. 从控制台创建了一个服务器密钥并将其添加到项目中。
  2. 从控制台启用Places API
  3. 从控制台启用Geocoding API

您可以在下图中看到。

当我运行代码时,它仍然向我显示上述错误。 我是否需要从控制台启用任何其他 API

除此之外,地图运行良好。如果您想知道这是我在清单中添加的 api 密钥。

<!-- Goolge Maps API Key Development mode -->
            <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAbo3luzbyQTuuPLOyo_Ml***********" />

我将服务器密钥直接添加到请求 url,如下所示

private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?key=" + API_KEY);
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));

        URL url = new URL(sb.toString());
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL", e);
        return resultList;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API", e);
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
        Log.e("result",jsonObj.toString());
        // Extract the Place descriptions from the results
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description")+ "," + predsJsonArray.getJSONObject(i).getString("place_id") );
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Cannot process JSON results", e);
    }
    return resultList;
}

请帮忙。

【问题讨论】:

  • 您是否创建了 google places api 密钥?
  • 我如前所述创建了服务器密钥,除此之外,地图工作正常。
  • 请发送您的 manifest.xml
  • @KaranMer 不是服务器密钥...您必须创建 google places api 密钥...
  • 没有像 google places api key 这样的东西。您可以从文档中看到您只需要服务器密钥developers.google.com/places/webservice/intro

标签: android google-maps-android-api-2 google-places-api autocompletetextview google-geocoding-api


【解决方案1】:

需要进行一些更彻底的研究,经过反复试验,它奏效了。主要是我需要启用 Google Places API 网络服务。它就像魅力一样。

希望这对那里的人有所帮助。

【讨论】:

  • 这是正确答案。由于您手动构建 HTTP 请求并将它们发送到 Web 服务端点,因此您需要启用 Web 服务 API。谷歌还有一个原生的 Android SDK 提供自动完成功能,值得研究:developers.google.com/places/android
  • 太棒了。你救了我的一天,伙计。 :)
  • 不错的答案。工作对我来说也很完美。只需要在启用 Google Places API Web Service 后为该 google 项目中的这个非 android 密钥生成服务器或浏览器密钥..
  • 完美运行。让我知道还需要 geocpding api 吗?
  • 答案是正确的,但似乎是 google places api for mobile 的主要故障
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 2019-05-21
  • 2016-10-29
  • 2019-03-03
  • 2015-09-21
  • 2014-06-03
  • 1970-01-01
  • 2016-01-09
相关资源
最近更新 更多