Subset of Google OAuth2 project from https://code.google.com/p/gtm-oauth2/