karan
karan

Reputation: 8853

Not getting Places data using google Places API - This API project is not authorized to use this API

I am using the code from the question asked here Autocomplete textview google places api description -> place_id. I changed the code to add it to my app. When I run the code it is giving me the below response when I run the code

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"}

I searched for some threads and from the below threads

  1. Maps API keys for geocoding not working
  2. This IP, site or mobile application is not authorized to use this API key and one or two other

I did the follow steps as suggested.

  1. Created a Server Key from console and added it to project.
  2. Enabled Places API from console.
  3. Enabled Geocoding API from console.

You can see in below image. enter image description here

and when I run the code it still shows me the error mentioned above. Do I need to Enable any other APIs from console.

The maps are working good except this. In case you want to know this is my api key added in manifest.

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

and I am adding server key directly to request url as below

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;
}

Please Help.

Upvotes: 0

Views: 2020

Answers (1)

karan
karan

Reputation: 8853

Needed some more thorough research and after some trial and error it worked. main thing was that I needed to enable Google Places API Web Service. and it worked like charm.

Hope this helps someone out there.

Upvotes: 6

Related Questions