5

I'm trying to get info from the API of Google Places for an Android application. To do that, first I have enabled this API in my Google Account.

enter image description here

enter image description here

Second, I have created an API KEY for Browser. I already have an API KEY Server due to another API.

enter image description here

So, in my code I have been tested with these two Keys and with both I've got always the same result!!!

{

"error_message" : "This service requires an API key.",

"html_attributions" : [],

"results" : [],

"status" : "REQUEST_DENIED"

}

The code that I'm using to make the call are ...

        @Override
    protected String doInBackground(LocationService... ls) {
        JSONObject result = new JSONObject();
        URL url;
        HttpsURLConnection urlConnection;

        // Making HTTP request
        try {

            //Define connection
            url = new URL("https://maps.googleapis.com/maps/api/place/nearbysearch/json");
            urlConnection = (HttpsURLConnection)url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("charset", "utf-8");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);

            //Send data
            String parameters = "?location=" + String.valueOf(ls[0].getLocation().getLatitude()) + "," + String.valueOf(ls[0].getLocation().getLongitude());
            parameters+="&radius=5000";
            parameters+="&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket";
            parameters+="&sensor=false";
            parameters+="&key=" + Constants.API_KEY_BROWSER_APPLICATIONS;
            byte[] postData = parameters.getBytes(Charset.forName("UTF-8"));
            int postDataLength = postData.length;
            urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            DataOutputStream data = new DataOutputStream(urlConnection.getOutputStream());
            data.write(postData);
            data.flush();
            data.close();

            Log.d(TAG, "Datos enviados");
            Log.d(TAG, "ResponseCode: " + String.valueOf(urlConnection.getResponseCode()));

            //Display what returns POST request

            StringBuilder sb = new StringBuilder();

            int HttpResult = urlConnection.getResponseCode();

            if(HttpResult == HttpURLConnection.HTTP_OK){
                String json;

                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                String line;

                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }

                br.close();

                //System.out.println(""+sb.toString());
                Log.d(TAG, "json: " + sb.toString());

                FileService file = new FileService();
                file.writeLog(POIActivity.TAG, getClass().getName(), POIActivity.urlConnection + parameters);
                file.writeLog(POIActivity.TAG, "doInBackground", sb.toString());

                // Parse the String to a JSON Object
                result = new JSONObject(sb.toString());

            }else{
                //System.out.println(urlConnection.getResponseMessage());
                Log.d(TAG, "urlConnection.getResponseMessage(): " + urlConnection.getResponseMessage());
                result = null;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.d(TAG, "UnsuppoertedEncodingException: " + e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "Error JSONException: " + e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "IOException: " + e.toString());
        }

        // Return JSON Object
        return result.toString();


    }

When I make the call to the API I've got like ResponseCode = 200 and the call that I build is finally like that ...

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY

Remember, like API_KEY I have used both, Api Key for server applications and Api Key for browser applications and I've got the same result with both.

Sincerely, I'm desperate with this problem because I don't know what I am doing wrong!!!

Oceinic
  • 406
  • 4
  • 15
José Carlos
  • 2,850
  • 12
  • 59
  • 95

3 Answers3

6

The problem is that you are not using the Google Places API for Android, you are using the Google Places API Web Service.

Here is an example of using Google Places API for Android, and here is an example of using the Google Places API Web Service. You are definitely using the latter.

Enable the Google Places API Web Service and it will work:

If you go to this link while signed into your Google Cloud Console account: https://console.cloud.google.com/apis/library?filter=category:maps

This is the API that should be enabled:

enter image description here

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank you @Daniel Nugent!!! I have enabled my Google Places API Web Service and ... still doesn't work!!! I think your are right, I have to use Google Places API Web Service and maybe there is something wrong in my code. To make the test I have used API Key Server and API Key Browser – José Carlos Jun 24 '15 at 00:06
  • 1
    Note that the documentation says that you need to use a Server Key, but I've used it with a Browser Key and it has also worked. Here is the documentation: https://developers.google.com/places/webservice/intro – Daniel Nugent Jun 24 '15 at 00:22
  • Yeees!!! I have launched this url https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY_SERVER with my API KEY Server on my browser and it works!!! But, If I try to get information through my Android application it doesn't work!!! :( – José Carlos Jun 24 '15 at 00:33
  • The error is the same: { "error_message" : "This service requires an API key.", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" } – José Carlos Jun 24 '15 at 00:38
  • 1
    Solved!!! I have modified my method to dowload data from Google Places API Web Service for the method that you recomend [here](http://stackoverflow.com/questions/30161395/im-trying-to-search-nearby-places-such-as-banks-restaurants-atms-inside-the-d/30162174#30162174) You have saved my life!! Thank you very much @Daniel Nugent!!! – José Carlos Jun 24 '15 at 00:49
  • That is marvellous! Thank you! – Rikki Tikki Tavi Feb 17 '16 at 08:25
1

From the documentation:

Note: You need an Android API key, not a browser key. You can use the same API key for your Google Maps Android API v2 apps and your Google Places API for Android apps.

Check this for more help.

Kevin Cronly
  • 1,388
  • 1
  • 13
  • 18
0

For an easier way try latest GCM configuration file implementation and easily create project using their developer interface.

Enable Google services for your app

strike
  • 1,031
  • 8
  • 24