1

I know that place API requires server key to use but I am using place SDK for android. I am using restricted Android API key (Restricted using package name and SHA1) but it is giving me below error:

This IP, site or mobile application is not authorized to use this API key. Request received from IP address 203.122.438.42, with empty referer

For reference, I am pasting my code below:

public PlacesSearchResponse fetch(com.google.maps.model.LatLng location) {
        PlacesSearchResponse request = new PlacesSearchResponse();
        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey(BuildConfig.MAPS_API_KEY)
                .build();
        try {
            request = PlacesApi.nearbySearchQuery(context, location)
                    .radius(5000)
                    .keyword("Shivam")
                    .type(PlaceType.BAKERY)
                    .await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return request;
            //Log.d("==", "=====" + request.results[0].name);
        }
    }

Dependency I am using:

> //To get near by locations
> implementation 'com.google.maps:google-maps-services:0.18.0'
> implementation 'com.squareup.okhttp3:okhttp:4.9.0'
Smeet
  • 4,036
  • 1
  • 36
  • 47

1 Answers1

1

You are not using Places SDK for Android. As far as I can see from your code you are using a Java wrapper for Google Maps web services. Web services are always intended to be used on server side and the only restriction that they accept is an IP address restriction on API key.

Have a look at this Java library in github (https://github.com/googlemaps/google-maps-services-java) and pay attention to the documentation, especially "Intended usage of this library" section. It states

The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys.

If you are building a mobile application, you will need to introduce a proxy server to act as intermediary between your mobile application and the Google Maps API Web Services. The Java Client for Google Maps Services would make an excellent choice as the basis for such a proxy server.

So, as you can see, the suggested by Google solution is introducing an intermediate proxy server where you can use this library with an API key restricted by IP address of your intermediate proxy server.

xomena
  • 31,125
  • 6
  • 88
  • 117
  • Thank you very much for detailed answer. Two questions: 1. Any example of proxy server to use restricted key that I can copy paste and use as it is ? 2. Is there any other way or android SDK to search particular near by places ? – Smeet Apr 02 '21 at 05:57