5

When using the Geocoding API from apex code on Salesforce developer account, I get the following error:

{
   "error_message" : "You have exceeded your daily request quota for this API.",
   "results" : [],
   "status" : "OVER_QUERY_LIMIT"
}

I know that mean I exceeded my daily request, but the problem is it's happens when I'm doing only few requests per day, it haven't worked once.

I'm only trying a simple use of the API, so no way I'm exceeding my daily requests (which are 2,500 requests per 24 hour period).

Maybe I'm missing something...

I set up a remote site for: http://maps.googleapis.com

Here is my test page:

<apex:page controller="clsHandleGeoLocation">
    <apex:form >
        <apex:pageBlock title="input address">
            <apex:pageblockSection columns="3">
                <apex:inputText value="{!Address}" label="Address"/>
                <apex:commandButton value="Submit" reRender="locPanel" action="{!getGeoLocation}"/>
            </apex:pageblockSection>
        </apex:pageBlock>

        <apex:outputPanel id="locPanel">
            <apex:outputText value="Location: {!Location}">
            </apex:outputText>
        </apex:outputPanel>
    </apex:form>  
</apex:page>

Here is my controller code:

public with sharing class clsHandleGeoLocation 
{

    private static Integer RequestTimeout = 6000;

    public String Address {get; set;}
    public String Location {get; set;}

    public clsHandleGeoLocation() 
    {
        Address = '';
        Location = '';
    }

    public PageReference getGeoLocation()
    {
        system.debug('START getLocation');

        try
        {
            // Callout.
            Http httpGetRequest = new Http();
            HttpRequest httpReq = new HttpRequest();
            httpReq.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address=' + Address + '&sensor=false');
            httpReq.setMethod('GET');
            httpReq.setTimeout(RequestTimeout);
            HttpResponse httpResponseAddress = httpGetRequest.send(httpReq);

            system.debug('res body: ' + httpResponseAddress.getBody());
            JSONParser parser = JSON.createParser(httpResponseAddress.getBody());
            Double latitude = null;
            Double longitude = null;

            /* 
               "location" : {
               "lat" : 37.42291810,
               "lng" : -122.08542120
              } 
            */
            while (parser.nextToken() != null)
            {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)
                    && (parser.getText() == 'location'))
                    {
                        // Jump to latitude and longtitude fields.
                        parser.nextToken();

                        while (parser.nextToken() != JSONToken.END_OBJECT)
                        {
                            String text = parser.getText();
                            parser.nextToken();
                            if (text == 'lat') latitude = parser.getDoubleValue();
                            else if (text == 'lng') longitude = parser.getDoubleValue();
                        }
                    } 
            }

            // Update Account coordinates.
            if (latitude != null)
            {
                Location += latitude + ', ' +  longitude;               
            }

        }
        catch(Exception e)
        {
            system.debug('Error');
        }

        return null;
    }
}  

Best Regards.

4

As dphil pointed out, using Google Geocoding API in a shared environment won't work.

Solution:

  1. Create an API Project: in Google Developer Console, just click on Create Project button.
  2. Assign Geocoding API to this project: click on 'APIs & auth' on the left menu, then search for Geocoding and click on the 'On/Off' button.
  3. Assign a Key for server applications for the project: click on 'Credentials' on the left menu, then under 'Public API access' click on 'Create New Key', choose 'Server key'.
  4. Copy the newly created key to the URL format, for example (note: when using key the url must be https):

    https://maps.googleapis.com/maps/api/geocode/json?key=MY_KEY&address=MY_ADDRESS&sensor=false

0

I searched for your error and came up with this:

https://stackoverflow.com/questions/19116352/whats-wrong-with-google-map-api

The comments speak of the requests going by IP address (The Salesforce address). The business version executes by clientId instead of IP. I believe you have to either somehow get a clientId or see if you can make Salesforce spoof where you are accessing the API from.

Essentially, you are sharing those 2500 requests with EVERYONE who uses SFDC.

  • Thanks, you probably right relevant to my specific example, but my original test was from: link which shouldn't have no problem to work, nevertheless I had the same error message, and it was before I try my sample. – GoldenAxe Feb 11 '14 at 17:41
  • I edited my response with the new information you gave me. – dphil Feb 11 '14 at 18:07
  • Thanks, I was afraid of that! You telling me that I share my API requests to Google Geocoding with everyone who uses Salesforce with Geocoding (without Google API business id)? Can you provide me a link that support this (I can't seems to find a formal info which specific telling me this)? – GoldenAxe Feb 11 '14 at 18:34
  • The link I already posted above gives you that info. It's not formal info from the Google service. I'm not sure where to find that, but they seem to have done their homework explaining how they came to that explanation in the comments of the link. – dphil Feb 11 '14 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.