Category Archives: Google APIs

Finding the ID’s of All Calendars attached to a Google Account

14 Sep 2015

If you work with Google Calendars you know that you need to specify the ID of a calendar in order to be able to update it. A small modification to the code provided by Google will print all your calendar objects to the console and then you can find the specific calendar’s ID.

Be sure to read this over at Google: Authentication using the Google APIs Client Library for JavaScript. Specifically you need to generate the access keys for your application before you can call the calendar.calendarList.list method to see all the calendars in your account.

I won’t walk through the whole thing – see below and note the comments (As an aside this was relevant to something I did that inserted calendar entries into a Google calendar – see this repo).


    var CLIENT_ID = 'your_client_id'
    var SCOPES = ['https://www.googleapis.com/auth/calendar'];


    // Check if current user has authorized this application.
    function checkAuth() {
        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES,
            'immediate': true
          }, handleAuthResult);
    }


    //* Handle response from authorization server.
    //* @param {Object} authResult Authorization result.
    function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
          authorizeDiv.style.display = 'none';
          loadCalendarApi();
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
    }


    //* Initiate auth flow in response to user clicking authorize button.
    //* @param {Event} event Button click event.
    function handleAuthClick(event) {
        gapi.auth.authorize(
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
          handleAuthResult);
        return false;
    }
	  

    //* Load Google Calendar client library. List upcoming events
    //* once client library is loaded.
    function loadCalendarApi() {
        // Now get the list of calendars for the primary google account
        gapi.client.load('calendar', 'v3', function() {
            var request = gapi.client.calendar.calendarList.list({
		 'calendarId': 'primary'
		 });
		 request.execute(function(resp) {
		 for (var i = 0; i < resp.items.length; i++) {
		    console.log(resp.items[i]);
                }
            });
        });
    }

Also, place the following in your html:

    <div id="authorize-div" style="display: ">
      <span>Authorize access to Google Calendar API
      <!--Button for the user to click to initiate auth sequence -->
      <button id="authorize-button" onclick="handleAuthClick(event)">
        Authorize
      </button>
    </div>
    <pre id="output"></pre>

Using the Google Maps API to Get Locations from Zip Codes

14 Sep 2012

Google offers many API’s, among them is the Maps API. In this example we’ll show all the code necessary to hit Google with a zip code to get the location in the form of City, State and Country.

As of June 22, 2016 you **must** apply for a Google Maps API key. All sites that use the service prior to June 22 will be grandfathered into the Google Maps service for keyless usage (Read this post from the Google Geo Developers Blog). Any new applications must include an API key so that Google can enforce rate limiting. If you see this error message: “MissingKeyMapError” – you are trying to use the Google Maps Service without a key – the fix is simply to acquire and start using an API key.

First thing to do is to reference Google’s Map API:

...

...

Next is a little form used to enter your zip code:

...
zip: Get Address
...

You can see that I have a link which fires a function called “getLocation()” – below you’ll find that function and the related code:

...


...

Thats it - open up the console in Chrome and notice the output as you enter different zip codes.