Friday, April 22, 2011

Google Latitude : Ruby on Rails Example using Oauth

 Click Like If you Agree

Spent a few days searching for  Google Latitude example
RUBY on RAILS and I am happy to say finally found a
good post on Google Latitude . I thought it would be great
to include this one in  my blog as it already contained a lot of
stuff on Google Latitude .

Register Domain with Google

1) Go to:
    Add a domain name if you have or
    Get cheap and good web hosting ($7.99 per year)
    Thats the cheapest price you can get on the web with
    a large no of of features .
    Hosting Includes 99.9% Uptime, RV Site Builder, 
    Fantastico Deluxe Scripts, Softaculous Auto Script  
    Installer, Ruby On Rails, Ruby Gems, Virus Scanner,
    WHM cPanel w/ZamFoo 7.1, CGI, Perl, PHP 5, cURL &
    GD, suPHP. 
    They are not that famous I guess but definitely a good one

 After registering your domain , the next step is to create 
 an pair of RSA keys , this one is needed for encryption
 of the data to be send during the OAuth Handshake .

In linux and OS X you can use openssl
openssl req -x509 -nodes -days 365 -newkey rsa:1024 
-sha1 -subj ‘/C=US/ST=CA/L=San 
Francisco/’ -keyout rsakey.pem -out 

Upload the rsacert.pem, and keep the rsakey.pem in a

safe place.  For the Target URL path prefix, Just add 
the domain name. 

Get the Oauth Consumer Key and Secret .

2) OAuth GEM

Include in your environment.rb

config.gem "oauth"
You need to install it to use in your system .
Recommended version 0.4.1 .

3) Generate OAuth Access Toke Model 

$ ruby script/generate model OAuthAccessToken
Note: I've done this because I don't want to store 
my access token directly in my User model.  This
gives me the option of having tokens for multiple 
scopes for a user through a polymorphic association.


create_table :oauth_access_tokens do |t|
      t.string  :oauth_token, :oauth_secret, :scope
      t.string  :subject_type, :limit > 30 #for polymorphic association
      t.integer :subject_id

4)User Model 

has_one  :latitude_access_token, 
:model_name => 'OAuthAccessToken',
:as => :subject, :conditions => ['scope = ?','latitude']

5)Setup OAUTH Consumer for Latitude

Create class lib/o_auth_consumers.rb

class OAuthConsumers  

  Latitude = CONSUMER_KEY, 
      :site => "",
      :request_token_path => "/accounts/OAuthGetRequestToken",
      :access_token_path => "/accounts/OAuthGetAccessToken",
      :authorize_path=> "/latitude/apps/OAuthAuthorizeToken",
      :signature_method => "RSA-SHA1",
      :private_key_file => "#{RAILS_ROOT}/config/rsakey.pem" 
       #path to my rsakey that I saved earlier


Remember to change the CONSUMER_KEY and 
CONSUMER_SECRET which you got from step1.

6)Generate Controller for APP

ruby script/generate controller latitude

require 'oauth'
require 'oauth/signature/rsa/sha1'
class LatitudeController < ApplicationController

def index
    #check the user has a latitude oauth access token
    if current_user.latitude_access_token
      token =,     current_user.latitude_access_token.oauth_secret)
      @request = JSON.parse(OAuthConsumers::Google.request('get', "", token).body)
      @lat = @request['data']['latitude']
      @lng = @request['data']['longitude']
      @time =['data']['timestampMs'].to_i)
       /1000).strftime('%b %d, %Y - %T')
      redirect_to :action => 'authorise'

 def authorise
 if !params[:oauth_token]

request_token = OAuthConsumers::Latitude.get_request_token(
{ :oauth_callback => "http://localhost:3000/latitude/authorise" },
{ :scope => "" }

session[:oauth_secret] = request_token.secret
      redirect_to request_token.authorize_url + 
      request_token =
        access_token = 
         :oauth_verifier => params[:oauth_verifier] )
        redirect_to :action => 'index'

if access_token
        #now save the access token for this user 
        OauthAccessToken.create :subject => current_user,
                                :scope => 'latitude',
                                :oauth_token => access_token.token,
                                :oauth_secret => access_token.secret

   redirect_to latitude_path

7) Get the Latitude To Work 

That's pretty much it.  Go to http://localhost:3000/latitude,
you will be directed to Google to authorise your account, 
then once that all goes through, you will be sent back to 
your app, an Access Token will be created which will be 
stored for your user.  I'm not exactly sure how long this 
lasts at the moment, but I'm testing that right now and 
will update if I find anything important.

Thanks to Ben Petro for creating the code on Ruby On Rails 
 platform .


  © Blogger templates The Professional Template by 2008

Back to TOP