Friday, February 01, 2013

Interfacing with the Google Data API from PowerBuilder.Net

Recently somebody asked me for help getting PowerBuilder.Net (and eventually PowerBuilder Classic through a COM Callable Wrapper) to interface with Google Calendar.  That actually turned out to be fairly simple because Google provides a .Net library for Google Data API which provides access not only to Google Calendar, but to a number of other Google services as well, such as Blogger, Contacts, Picasa Web Albums, Spreadsheets and YouTube.

We're going to see just how easy by accessing Google Calendar and retrieving our events for the next few days.  To do that, first download and install the Google Data API .net libraries (you only need the first one on the list).  Then create a PowerBuilder.Net WPF project and add references to the Google.GData.AccessControl, Google.GData.Calendar, Google.GData.Client and Google.GData.Extensions assemblies.
references.PNG
You'll want to create a DataWindow to hold the results of the query. For this sample, I just created an external source datawindow with two columns, one to hold the event date/time (eventdate) and another to hold the title of the event (eventtitle).

Once you've done that, add a command button to the window and add code similar to the following to it's clicked script.  You'll need to add your own Google email and password information:

  1. Google.GData.Calendar.CalendarService service  
  2. Google.GData.Calendar.EventQuery query  
  3. Google.GData.Calendar.EventFeed calFeed  
  4. System.Collections.IEnumerator entries   
  5. Google.GData.Calendar.EventEntry entry  
  6. System.Collections.IEnumerator dates  
  7. Google.GData.Extensions.When date  
  8. string        title  
  9. datetime start  
  10. long     row  
  11. service = create Google.GData.Calendar.CalendarService("PBCalendarSampleApp")  
  12. service.setUserCredentials(<gmail address>, <password>)  
  13. query = create Google.GData.Calendar.EventQuery()  
  14. query.Uri = create System.Uri("https://www.google.com/calendar/feeds/default/private/full")  
  15. query.StartTime = System.DateTime.Now.AddDays(-28)  
  16. query.EndTime = System.DateTime.Now.AddMonths(6);  
  17. calFeed = service.Query(query)  
  18. entries = calFeed.Entries.GetEnumerator()  
  19. do while entries.MoveNext()  
  20.      entry = entries.Current  
  21.      title = entry.Title.Text  
  22.      dates = entry.Times.GetEnumerator()  
  23.      do while dates.MoveNext()  
  24.          date = dates.Current  
  25.          start = date.StartTime  
  26.          row = dw_1.InsertRow(0)  
  27.          dw_1.Object.eventtitle[row] = title  
  28.          dw_1.Object.eventdate[row] = start  
  29.      loop  
  30. loop  

So, here's my sample calendar for the demo:
googlecalendar.PNG
And here's the WPF app PowerBuilder.Net created accessing the data.
wpfapp.PNG

No comments: