Question

I am building a java web application that requires googles geocoding service as defined on https://developers.google.com/maps/documentation/geocoding/ . My application is all setup and everything works, I am now attempting to sign my requests to allow for additional features from the API such as additional rate limits billing ect...

The problem I am having is I cant seem to get a proper Client Id and Key that works with the geocoding API. I used just about every sensible combination of id and key from the API Access tab on Google's API console. To test my credentials and I have copied the PHP code directly from google at http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/index.html to ensure my requests are signed correctly. I watched a google developers video about this topic (https://www.youtube.com/watch?v=ODeXo7YIwVg) and it shows this being done for a Google business account, After reading through Google's API docs it would seem you do not have to have a business account to use the geocoding service however I cant seem to get a valid client ID and Key to work for me. Does anyone know of the correct setup to use this service on a non business account? or is it even possible? (keep in mind this a server to server connection)

If anyone needs additional information let me know. I would be grateful for any suggestions.

edit: Looking at the Usage Limits listed on https://developers.google.com/maps/documentation/geocoding/#Limits you see it implies that you can use the Geocoding API without a business account.... but how?

Was it helpful?

Solution

I went through the documentation and as you have rightly pointed out you do not need a business account to use the Geocoding API. If you have a non business account then you only need to pass the API key. Client ID is required only if you have a business account. The key itself is unique as mentioned below :

https://developers.google.com/console/help/#WhatIsKey

An API key is a unique key that you generate using the Console. When your application needs to call an API that's enabled in this project, the application passes this key into all API requests as a key=API_key parameter. Use of this key does not require any user action or consent, does not grant access to any account information, and is not used for authorization.

According to the link below the Client ID is used instead of key in the case of Business Accounts.

https://developers.google.com/maps/articles/v2tov3#newkey

If you are a Maps for Business customer, you will need to use a Client ID in place of a Key. Note that the two cannot be used together. Client IDs are similar to keys, but with the following differences:

  • Maps for Business applications using a Client ID may have access to additional features or limits, such as Maps Analytics.Your Client ID will be given to you by Google Enterprise Support. You do not need to use the API Console.

  • When loading the Google Maps JavaScript API, you will use the client parameter instead of the key parameter.

I'm guessing the same principle applies to the Geocoding API. If you want the additional feature that are not part of non business account you will have to get a Business Account.

I ran the following code using only the key to sign my request in Java and it works perfectly.

    public static String getAddress(String latitude, String longitude){
    String address = null;
    try {
        final String inputurl = "http://maps.googleapis.com/maps/api/geocode/xml?latlng="+ latitude + "," + longitude + "&sensor=true";
//          Sign the URL using key
        String url = getRequestURL(inputurl);
        url = "http://maps.googleapis.com" + url;

        final URL google = new URL(inputurl);
        final URLConnection geocode = google.openConnection();
        final BufferedReader in = new BufferedReader(new InputStreamReader(
                geocode.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            address += inputLine;
        }
        in.close();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return null;
}

public static String getRequestURL(final String inputurl) throws IOException,
InvalidKeyException, NoSuchAlgorithmException, URISyntaxException {
    // Convert the string to a URL so we can parse it
    final URL url = new URL(inputurl);

    final URLSigner signer = new URLSigner("*INSERT YOUR KEY HERE*");
    final String request = signer.signRequest(url.getPath(), url.getQuery());

    return request;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top