Search code examples
androidhttpapi-key

Why is my project not authorized to use an API key?


I generated API key for my android app that requires map and current location information. API key must be fine because I can access current location and map. But when I tried the following code:-

 public void run(){
        String addressURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
                +Double.toString(lat)+","+Double.toString(lon)+"&sensor=true&key="+getString(R.string.api_key);
        Log.d(Globals.TAG, "Request to = " + addressURL);
        httphandler = new HttpHandler(addressURL);
        httphandler.addHttpLisner(MapsActivity.this);
        httphandler.sendRequest();
    }

   if (http.getResCode() == HttpURLConnection.HTTP_OK
                    || http.getResCode() == HttpURLConnection.HTTP_ACCEPTED) {
                String resp = http.getResponse();
 }

I get resp = { "error_message" : "This API project is not authorized to use this API.", "results" : [], "status" : "REQUEST_DENIED" }

I don't know why? I am sharing my google console API key settings pictures:-

enter image description here

enter image description here

enter image description here

Is my API key setting correct? How to use API key for HttpHandler?


Solution

  • in order to extend the API restrictions, you have to enable the Geocoding API first ...

    then it can be assigned as an "API Restriction" (required because of other restrictions).

    for HTTPS REST API access, you probably might need no "Application Restrictions",

    because this basically equals a web-application accessing the API from dynamic IP ...

    nevertheless the Geocoding API needs to be enabled for the project and permitted for the key.

    if you want IP restriction, you'd have to call with your app to your own web-service, having static IP.