1) Register domain with Google Webmaster Central
Go to: https://www.google.com/accounts/ManageDomains
Add domain, then verify it.
Create a pair of RSA keys. 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/CN=example.com’ -keyout rsakey.pem -out rsacert.pem
Upload the rsacert.pem, and keep the rsakey.pem in a safe place. For the Target URL path prefix, I just added my domain. It's not important.
Once the domain is registered, you will see an OAuth Consumer Key and Secret. You need this for your app.
Now to your App
2) OAuth Gem
Include in your environment.rb
config.gem "oauth"Obviously you will need to install it on your system if you don't already have it. I use version 0.4.1
3) Generate an OAuthAccessToken model
$ ruby script/generate model OAuthAccessTokenNote: 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.
Migration:
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
t.timestamps
end
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 = OAuth::Consumer.new( CONSUMER_KEY, CONSUMER_SECRET, {
:site => "https://www.google.com",
: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
})
end
Remember to change the CONSUMER_KEY and CONSUMER_SECRET according to the information supplied when you registered your Domain (Step 1)
6) Generate a controller for the 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 = OAuth::Token.new(current_user.latitude_access_token.oauth_token, current_user.latitude_access_token.oauth_secret)
@request = JSON.parse(OAuthConsumers::Google.request('get', "https://www.googleapis.com/latitude/v1/currentLocation", token).body)
@lat = @request['data']['latitude']
@lng = @request['data']['longitude']
@time = Time.at((@request['data']['timestampMs'].to_i)/1000).strftime('%b %d, %Y - %T')
else
redirect_to :action => 'authorise'
end
end
def authorise
if !params[:oauth_token]
request_token = OAuthConsumers::Latitude.get_request_token(
{ :oauth_callback => "http://localhost:3000/latitude/authorise" },
{ :scope => "https://www.googleapis.com/auth/latitude" }
)
session[:oauth_secret] = request_token.secret
redirect_to request_token.authorize_url + "&domain=example.com&granularity=best&location=all"
else
request_token = OAuth::RequestToken.new(
OAuthConsumers::Latitude,
params[:oauth_token],
session[:oauth_secret]
)
begin
access_token = request_token.get_access_token( :oauth_verifier => params[:oauth_verifier] )
rescue
redirect_to :action => 'index'
end
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
end
redirect_to latitude_path
end
end
end
7) Enjoy the Latitude
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:
- http://www.manu-j.com/blog/add-google-oauth-ruby-on-rails-sites/214/
- http://oauth.rubyforge.org/rdoc/
- http://code.google.com/apis/accounts/docs/OAuth.html
- http://stakeventures.com/articles/2008/02/23/developing-oauth-clients-in-ruby
- http://code.google.com/apis/latitude/v1/using_rest.html
Interested in your thoughts and comments. Let me know if my code sucks.