Friday, December 21, 2012

Ruby meets the Google Calendar API (or Ruby vs. Google API)

Setup: Windows 7 (64Bit) with Ruby 1.9.3p125 (Railsinstaller 2.1.0 for Windows) and google-api-client 0.5.0

Some time ago i tried to access the Google Calendar via a small Ruby script. I found some frameworks that should do the job but realized at some point that none of the frameworks covered the complete Google Calendar API (e. g. you couldn’t delete an event). I put the idear by side and didn’t thought much about it.

Last week i tried it again. This time i thought i will try the ruby api client from Google directly. So i installed the google api client (Setup instructions can be found here):

gem install google-api-client
Configured a Service account with the Google API Console (more infos here).

I took one of the examples from Google (analytics.rb) and modified it slightly to use the Calendar API instead of the Analytics API.

require 'rubygems'
require 'google/api_client'
require 'json'

key_file = "privatekey.p12"
key_secret = "notasecret"

client = Google::APIClient.new()

#This call leads to an "Invalid keyfile or passphrase (ArgumentError)" Exception
key = Google::APIClient::PKCS12.load_key(key_file, key_secret)

asserter = Google::APIClient::JWTAsserter.new(
   '###################################@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/calendar',
   key)

client.authorization = asserter.authorize()
service = client.discovered_api('calendar', 'v3')

result = client.execute(
       :api_method => service.calendar_list.list,
       :headers => {'Content-Type' => 'application/json'})

parsed = JSON.parse(result.data.to_json)

parsed["items"].each { |item| puts item["summary"] }
This leads to an "Invalid keyfile or passphrase (ArgumentError)" exception from google-api-client-0.5.0/lib/google/api_client/auth/pkcs12.rb:43 (BTW: This script works perfectly on Linux!)

I took a closer look at the File pkcs12.rb and since there is nothing special in here i just copied the code and removed the exception handling to see what is really going on here.

require 'rubygems'
require 'google/api_client'
require 'json'

key_file = "privatekey.p12"
key_secret = "notasecret"

client = Google::APIClient.new()

content = File.read(key_file)

#The gives the PKCS12_parse Error
pkcs12 = OpenSSL::PKCS12.new(content, key_secret)
key = pkcs12.key

asserter = Google::APIClient::JWTAsserter.new(
   '###################################@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/calendar',
   key)

client.authorization = asserter.authorize()
service = client.discovered_api('calendar', 'v3')

result = client.execute(
       :api_method => service.calendar_list.list,
       :headers => {'Content-Type' => 'application/json'})

parsed = JSON.parse(result.data.to_json)

parsed["items"].each { |item| puts item["summary"] }
this time i got a “PKCS12_parse Error: mac verify failure”. So now i got a different Error. I searched the net an found a hint on Stackoverflow. The cause of the Error is that ruby on Windows by default read the keyfile as text but it must be read as binary. Well the given solution for the question is not working with ruby 1.9.3 but i found another way:
require 'rubygems'
require 'google/api_client'
require 'json'

key_file = "privatekey.p12"
key_secret = "notasecret"

client = Google::APIClient.new()

#We must tell ruby to read the keyfile in binary mode.
content = File.read(key_file, :mode => 'rb')

pkcs12 = OpenSSL::PKCS12.new(content, key_secret)
key = pkcs12.key

asserter = Google::APIClient::JWTAsserter.new(
   '###################################@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/calendar',
   key)

client.authorization = asserter.authorize()
service = client.discovered_api('calendar', 'v3')

result = client.execute(
       :api_method => service.calendar_list.list,
       :headers => {'Content-Type' => 'application/json'})

parsed = JSON.parse(result.data.to_json)

parsed["items"].each { |item| puts item["summary"] }
Now i get a list of all Calendars for the E-Mail address of the Service Account. Great. But i want to work with the Calendar from my normal google account and not the one for the Service Account.

The Solution is easy: Share which ever Calendar you want to access with the E-Mail address of the Service Account.

Now you can access the Calender and for example list all events.

Google Calendar API Reference:
https://developers.google.com/accounts/docs/OAuth2?hl=de#scenarios Some information about OAuth 2.0 to Access Google APIs:
https://developers.google.com/accounts/docs/OAuth2?hl=de#scenarios

Wednesday, November 28, 2012

“Shipping Greatness” by Chris Vander Mey; O’Reilly Media

Chris Vander Mey is a former Google product manager and Amazon engineering manager that has held multiple roles in small company startups. He has a Masters in Engineering Management from Dartmouth College and a BS in Aerospace Engineering from University of Virginia.

The Book has 13 chapters which are divided in two parts and three short appendices with further information including references for further reading.


Part one (chapters 1-7) is about the process to ship great Software and part two (chapters 8-13) is about the needed skills for any project leader.


It's target audience are managers of bigger companies. It explains the whole life cycle of a new Software product from the initial idea up to shipping the product.

In my opinion the reader should already have some knowledge about product management before reading this book.
Chris gives a lot of tips and examples from his own experience at Google and Amazon.

After all i think the Book is a bit to short to cover all the topics adequate.
I am not sure if like the book or not. I don’t like Chris writing style and IMHO he uses to many superlatives.

You can find more infos at the O’Reilly product page here.

Wednesday, November 14, 2012

Jelly Bean 4.2 for Nexus 7

This morning I got a system update (Jelly Bean 4.2) for my Nexus 7 :-) Here are my first impressions: After the reboot you see that there is now some kind of Widgets for the lock screen. You can choose between Clock, Calendar and Gmail.

After logging in I wanted to check the setting of my tablet. I pulled down the notification bar and ... realized that the Settings button is gone. I remembered that I read something about the new "Quicksetting" Feature. You must wipe with two one fingers from top to Bottom in the Info bar (that's where you see the current time and the small Battery icon).

I took a quick look through the available settings and checked out the new "Daydream" which you find in the Display Options.

The "Developer options" in the settings are not visible by default. To enable them, you need to go to "About Tablet" in the settings, scroll down to the "Build number" and tap it seven times, yes I know that sounds crazy. When you go back, you will see an entry for the "Developer options".

Next thing I checked out was the new Android Keyboard. I am not sure about this. Wiping to write is really fast - but only if the Keyboard recognizes the words you are trying to write correctly, which it didn't in my case. I couldn't get it to write the same letter twice in one word, so I switched back to the SwiftKey Keyboard.
I will give the Android Keyboard another try when I have a bit more time.

After the update my Amazon apps (Appstore, Amazon Kindle, Amazon MP3) couldn't remember my Amazon credentials any more.

Another thing I realized was that my custom wallpaper doesn't seem to fit the screen any more. When I scroll to the most left or right Home screen there is a 1 cm gap without wallpaper. Looks like they changed the resolution for the wallpaper.

Oh, and Firefox crashes a lot under Jelly Bean 4.2 :-(

Saturday, September 8, 2012

Visual Studio Conditional Breakpoints Pitfalls

Conditional Breakpoints in Visual Studio are great. They allow you to restrict when the breakpoint will hit based on the evaluation of a Boolean expression. You can choose to hit the breakpoint when the condition is true or when the result of the condition has changed.

But there are a couple of things you should be aware of:
  • that the source language determines the expression operators. This means that if you are debugging in Visual Basic, the “<>” operator means “not equal” while in C# or C++/CLI, you would use “!=”.
  • if you choose a wrong expression operator you get no warning or error. Assume you use “=” (assign) instead of “==” (equal) and you will see that suddenly the variable you are examine will always have the same value.
So do yourself a favor and double check the conditions of your breakpoints.

More infos about conditional Breakpoints:
http://msdn.microsoft.com/en-us/library/7sye83ce.aspx
http://support.microsoft.com/kb/308469

Friday, September 7, 2012

"Think Like a Programmer" by V. Anton Spraul; No Starch Press

V. Anton Spraul is an experienced computer science teacher and has taught introductory programming and computer science for more than 15 years.

The goal of this book is to teach you the creative Part of programming. It's target audience are clearly beginners who "struggle to write programs, even though you think you understand programming languages." You should be familiar with at least one Programming language (it doesn't have to be c++).

The book starts with some general problem solving techniques (e. g. always have a plan, divide and conquer) and explains them on some general puzzles.
In later chapters Problems will be solved with specific programming concepts like Arrays, pointers, dynamic memory and recursion.

You might be disappointed because Spraul doesn't give any answers to the exercises but as Spraul states at the beginning of the book you should see them as opportunities to apply the concepts described in the chapters.

The exercises are coded in c++ but the book is not specifically about c++. Most chapters start with a short review of the used concepts in c++.

More infos can be found here

Monday, July 30, 2012

"Team Geek" by Brian W. Fitzpatrick and Ben Collins-Sussman; O'Reilly Media;

Brian Fitzpatrick leads Google's Data Liberation Front and Transparency Engineering teams.
Ben Collins-Sussman is one of the founding developers of SVN and now manages the engeneering team for the Google Affiliate Network.
Both have a lot of experience with Open Source Projects.

The Book has a clearly defined goal - to help programmers become more effective and efficient at creating software by improving their ability to understand, communicate with, and collaborate with other people.
And that is the essence of this book. It explains why each relationship (not only related to Software projects) should be based on Humility, Respect and Trust (HRT).

The message of the book also applies to the relationship between team mates, team leader and team and above all to the relationship with end users. The book gives useful tips on how to cope with complicated team mates and how managers should lead their team.

Brian and Ben explain why a team culture is so important and should be protected right from the start. Last but not least the reader gets some tips on how to promote himself better within his company. I really enjoyed reading this book.

Book catalog page

Tuesday, July 17, 2012

List<T>.BinarySearch Pitfalls

The BinarySearch Method of List collection can be very useful to get the zero-based index of an element.
Further more if the element you searched for is not in the List you can calculate the index of the first element that is larger then the element you searched for. Just apply the ~ operator on the result of the BinarySearch:
var pos = aList.BinarySearch(aString);
if (pos < 0)
{
    aList.Insert(~pos, aString);
}

But there is one pitfall: The List must already be sorted; otherwise the BinerySearch result is incorrect.
This is not a big secret. It is clearly stated in the MSDN description for the Method (under Remarks)

I saw a Bug in a Project where someone used a BinarySearch to find duplicated entries between to Lists. This works as long as the Lists are sorted. If not the result is totally nonsense.
It took a while to find this Bug.
MSDN Link:
http://msdn.microsoft.com/en-us/library/w4e7fxsh.aspx