Authentication using Google Accounts for your Rails App

The methodology is very simple.

  1. Redirect the user to grant access to your app for a particular Google service.
  2. Once access is granted, use the authentication token to retrieve information related to the user.

I demonstrate this with a very simple & idiotic Sinatra app which actually does nothing intelligent – apart from just outputting first 10 contacts from the user’s address book.

  1. require ‘rubygems’
  2. require ‘sinatra’
  3. require "gdata"
  4.  
  5. client = GData::Client::Contacts.new
  6.  
  7. get "/" do
  8.   next_url = ‘http://127.0.0.1:4567/app’
  9.   secure = false
  10.   session = true
  11.   authsub_link = client.authsub_url(next_url, secure, session, nil)
  12.   redirect(authsub_link)
  13. end
  14.  
  15. get ‘/app’ do
  16.   pp request.env
  17.   client.authsub_token = request.env["rack.request.query_hash"]["token"]
  18.   client.authsub_token = client.auth_handler.upgrade()
  19.   contacts = client.get("http://www.google.com/m8/feeds/contacts/default/full?max-results=5").to_xml
  20.   puts contacts
  21. end

More information about using GData is available at http://code.google.com/apis/gdata/articles/gdata_on_rails.html

Example scenarios this can be used?

  • Allowing people to login into your app with their existing google account.
  • Accessing user data with user’s permission
  • Signing up for your app using Google account information

Happy Hacking!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  • http://topsy.com/tb/bit.ly/K4TAn Tweets that mention Dare to Dream? » Blog Archive » Authentication using Google Accounts for your Rails App — Topsy.com

    [...] This post was mentioned on Twitter by makuchaku. makuchaku said: Just blogged – Authentication using Google Accounts for your Rails App http://bit.ly/K4TAn [...]