Search code examples
javaandroidgoogle-geolocation

Android Google Geolocation api return 403 error


i m trying to build android app that can get the location of user from Google Geolocation Api. I configuration and setup billing account

My Setting are

  1. Increase quota to 100/request/user
  2. Billing account Setup
  3. Allowed ip : any

i m using Async Task class

   // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY");

                String json = "";

                // 3. build jsonObject
                JSONObject jsonObject = new JSONObject();
                jsonObject.accumulate("homeMobileCountryCode", 310);
                jsonObject.accumulate("homeMobileNetworkCode", 410);
                jsonObject.accumulate("radioType", "gsm");
                jsonObject.accumulate("carrier", "vodafone");

                JSONArray cellTowersArray = new JSONArray();;
                JSONObject cellTowerObject = new JSONObject();;
                cellTowerObject.accumulate("cellId", 42);
                cellTowerObject.accumulate("locationAreaCode", 415);
                cellTowerObject.accumulate("mobileCountryCode", 310);
                cellTowerObject.accumulate("mobileNetworkCode", 410);
                cellTowerObject.accumulate("age", 0);
                cellTowerObject.accumulate("signalStrength", -60);
                cellTowerObject.accumulate("timingAdvance", 15);
                cellTowersArray.put(cellTowerObject);
                jsonObject.accumulate("cellTowers", cellTowersArray);

                JSONArray wifiAccessPointsArray = new JSONArray();
                JSONObject wifiAccessPointObject = new JSONObject();
                wifiAccessPointObject.accumulate("macAddress", "01:23:45:67:89:AB");
                wifiAccessPointObject.accumulate("age", 0);
                wifiAccessPointObject.accumulate("channel", 11);
                wifiAccessPointObject.accumulate("signalToNoiseRatio", 40);
                wifiAccessPointsArray.put(wifiAccessPointObject);
                jsonObject.accumulate("wifiAccessPoints", wifiAccessPointsArray);

                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();


                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);;

                // 6. set httpPost Entity
                httpPost.setEntity(se);

                // 7. Set some headers to inform server about the type of the content
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);

                // 9. receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // 10. convert inputstream to string
                if(inputStream != null)
                    result = convertInputStreamToString(inputStream);
                else
                    result = "Did not work";
            }
            catch (MalformedURLException e) {
            	 Log.e("YOUR_APP_LOG_TAG1", "", e);
            }
            catch (IOException e) {
            	 Log.e("YOUR_APP_LOG_TAG2", "", e);
            }
            catch (Exception e) {
            	 Log.e("YOUR_APP_LOG_TAG3", "", e);;
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            Log.d("result", result);
            
        }
    

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
          BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));;
          String line = "";
          String result = "";
          while((line = bufferedReader.readLine()) != null)
              result += line;

          inputStream.close();
          return result;
    }   
	

but i m still getting 403 Error, allowed Ip is not Configuration

Help/ Suggestion are appreciated.thanks


Solution

  • I figure it our Myself. if you are having trouble using Google gelocation Api and getting 403 error. then you have to follow these steps . it is Simple :)

    1. decrease the no of API request to 0
    2. then disable the Geolocation API
    3. Create a New Project
    4. Then enable the Google Geolocation Api
    5. Add / increase the No of request of API
    6. Create the Android API Key
    7. Server API key
    8. Use it

    and you are good to go , now you can use your google geolocation api with ease

    Hope it will help thanks