简体   繁体   中英

Why is my api key not accepted by the Google Directions (Android) request?

Whenever I include my Google Play Services API key along with my request for Google Directions like in the code below:

    private String makeDirectionsURL(double originLat, double originLong, double destLat, double destLong)
{
    StringBuilder url = new StringBuilder();
    //first part of url//
    url.append("https://maps.googleapis.com/maps/api/directions/json?");
    //start adding parameters//
    //origin coordinates
    url.append("origin="+originLat+","+originLong);
    //destiniation coordinates
    url.append("&destination=");
    url.append(destLat+","+destLong);
    //api key
    url.append("&key=");
    url.append(getResources().getString(R.string.google_api_key));
    //NOTE: for some reason, the request suceeds when leaving out the api key 

    return url.toString();

}

When I include the api key as a parameter in the request, the json response shows that my request has been denied. The response reads:

{ "error_message" : "This IP, site or mobile application is not authorized to use this API key.", "routes" : [], "status" : "REQUEST_DENIED" }

Yet the same api key works for my google map view - and my api console registers the accesses for the map view quota limit. Even odder yet, when I leave out the key parameter in the Directions request I get a valid JSON response.

I have a feeling that I have to generate another different api key for just the Google Directions service - but I'm not sure how to. The documentation here: https://developers.google.com/maps/documentation/directions/#api_key

Says to visit the console and activate the Directions API service - I did. Then it says my api key "will be available from the API Access page, in the Simple API Access section. Directions API applications use the Key for server apps." Now is where I'm confused - how can I use the key for server apps if I am accessing from a mobile device - I'm assuming this is for any webpages that wish to use the service - but what do I do for an android app. As I said, I already tried using my Simple API Access key for Android apps, which I know works, yet when I pass the same key to Google Directions - it mysteriously doesn't work...

Any help, vague guidance, or links to read up on would be really appreciated.

PS: If I can't figure this out - am I allowed to keep sending requests w/oa api key?

I think the issue is because you are trying to pass a server key which is directly tied to your server's IP address, when you should be passing an android app key .

In your Google APIs console, navigate to API's and Auth . From here you can create a new public API key.

创建Android密钥

When prompted, select Android Application 在此输入图像描述

You will need to enter your device's SHA1 Certificate Fingerprint

This comes from the keystore used to sign the apk. When running an app in debug, you will most likely be using your IDE's debugging keystore. Google has a nice write up on how to get this information.

When releasing the app, you will need to sign it with your own key. You will either need to edit your API key to the new keystore SHA1 fingerprint or create a new API key.

Additionally, according to Google , you should be using a key.

All Directions API applications should use an API key. Including a key in your request:

  • Allows you to monitor your application's API usage in the APIs
  • Console. Enables per-key instead of per-IP-address quota limits.
  • Ensures that Google can contact you about your application if necessary.

While I can't comment on how strictly they enforce this, I strongly encourage you to do so to avoid any issues down the road.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM