Authentication using Google Accounts for your Rails App
The methodology is very simple.
- Redirect the user to grant access to your app for a particular Google service.
- 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.
-
require ‘rubygems’
-
require ‘sinatra’
-
require "gdata"
-
-
client = GData::Client::Contacts.new
-
-
get "/" do
-
next_url = ‘http://127.0.0.1:4567/app’
-
secure = false
-
session = true
-
authsub_link = client.authsub_url(next_url, secure, session, nil)
-
redirect(authsub_link)
-
end
-
-
get ‘/app’ do
-
pp request.env
-
client.authsub_token = request.env["rack.request.query_hash"]["token"]
-
client.authsub_token = client.auth_handler.upgrade()
-
contacts = client.get("http://www.google.com/m8/feeds/contacts/default/full?max-results=5").to_xml
-
puts contacts
-
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.
View Comments to Authentication using Google Accounts for your Rails App
Tweets that mention Dare to Dream? » Blog Archive » Authentication using Google Accounts for your Rails App -- Topsy.com
September 28th, 2009 at 1:24 am
[...] This post was mentioned on Twitter by makuchaku. makuchaku said: Just blogged – Authentication using Google Accounts for your Rails App http://bit.ly/K4TAn [...]