Iban Arriola Iban Arriola - 3 months ago 45
Android Question

Use of google apis in android with restriction

I want to use the autocomplete provided by googleapis in my android app.

This is the code I am using for that:

private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "MY_API_KEY";

public static ArrayList autocomplete(String input) {
ArrayList resultList = null;

HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&input=" + URLEncoder.encode(input, "utf8"));

URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());

// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
} catch (MalformedURLException e) {
Log.e(TYPE_AUTOCOMPLETE, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(TYPE_AUTOCOMPLETE, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {

The problem is that I want to restrict my api_key to be used only in my android app. To do this I put a restriction of android apps with my apps SHA-1 certificate fingerprint. How can I use the autocomplete and put my fingerprint to validate it?


You can't be safe if you store a web key in your app. If you want to continue using an http request, the only way is to use a server components that acts like a proxy: get the request from your app, adds the API key, that is safely stored in your server, performs the request to google and sends back the result to your app.

But the autocomplete API is available for Android too. You can follow this tutorial