Skip to content

Instantly share code, notes, and snippets.

@sosedoff
Created May 19, 2011 19:59
Show Gist options
  • Save sosedoff/981594 to your computer and use it in GitHub Desktop.
Save sosedoff/981594 to your computer and use it in GitHub Desktop.
Goole OAuth2 omniauth strategy
require 'omniauth/oauth'
require 'multi_json'
module OmniAuth
module Strategies
class Google2 < OAuth2
def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
client_options = {
:site => 'https://accounts.google.com/o/oauth2',
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:access_token_url => 'https://accounts.google.com/o/oauth2/token'
}
google_contacts_auth = "http://www.google.com/m8/feeds"
options[:scope] ||= google_contacts_auth
options[:scope] << " #{google_contacts_auth}" unless options[:scope].include?(google_contacts_auth)
super(app, :google2, client_id, client_secret, client_options, options, &block)
end
protected
def user_data
@data ||= MultiJson.decode(@access_token.get("http://www.google.com/m8/feeds/contacts/default/full?max-results=1&alt=json"))
end
def user_info
email = user_data['feed']['id']['$t']
name = user_data['feed']['author'].first['name']['$t']
name = email if name.strip == '(unknown)'
{
'email' => email,
'uid' => email,
'name' => name
}
end
def auth_hash
ui = user_info
OmniAuth::Utils.deep_merge(super, {
'uid' => ui['uid'],
'user_info' => ui,
'extra' => {'user_hash' => user_data}
})
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment