Google Geocode API KEY Restriction for Mobile Applications

Viewed 2509

My android and iOS application was using same unrestricted (platform and service) api key for geocode distance matrix places services. I want to use separate restricted keys for separate service and platform. In google cloud console, how can I restrict my keys for different platforms? should I use and android giving package name and sha1 and iOS with bundle ID or should I use IP address of my server? what should be the best practice? I am using retrofit to hit these apis.

sometime keys are responding with this IP, site or mobile application is not authorized to use this API key. Request received from IP address 202.65.11.56, with empty referer and again after somethime it is responding with proper response. why is such abnormality happening?

I am also searching places using places autocomplete sdk and for getting geometry I am calling geocode using place_id, should I give places api permission in geocode api key for this?

2 Answers

For all the Google Api usages you are making, you can use a single ApiKey and restrict it. Restrict it based on your platform and use this key in the respective platforms. SInce you have mentioned that you will be using in Mobile Applications(Android & iOS), here are the steps.

Go to Google Cloud Console

Create a project > Go to APIs & Services > Credentials > Create Credential

Then restrict the key like this -

  1. For Android, add your Package name & SHA-1 certificate here

enter image description here

  1. For iOS, add your Bundle Id here

enter image description here

Now use the APIKey in android & iOS platform. This is the best way to restrict your Google API Key.

Hope it helps!

You are most likely using a web service with your API key that is restricted for Android/iOS Apps.

Due to the Mobile App restrictions, it can only be used for the Maps Android/iOS SDKs, and not any web service or JavaScript API.

What I recommend doing is identifying which web service is giving this error (probably Geocoding API, please double-check), and creating a separate API key with IP-address restrictions (not Android/iOS App), so that it can be used for the web service.

Related