Created
July 12, 2011 17:34
-
-
Save andersonleite/1078496 to your computer and use it in GitHub Desktop.
omniauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'omniauth/oauth' | |
require 'multi_json' | |
module OmniAuth | |
module Strategies | |
# | |
# Authenticate to orkut via OAuth and retrieve basic user info. | |
# | |
# Usage: | |
# | |
# use OmniAuth::Strategies::Orkut, 'consumerkey', 'consumersecret' | |
# | |
class Orkut < OmniAuth::Strategies::OAuth | |
def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block) | |
client_options = { | |
:site => 'https://www.google.com', | |
:request_token_path => '/accounts/OAuthGetRequestToken', | |
:access_token_path => '/accounts/OAuthGetAccessToken', | |
:authorize_path => '/accounts/OAuthAuthorizeToken' | |
} | |
super(app, :orkut, consumer_key, consumer_secret, client_options, options) | |
end | |
def auth_hash | |
ui = user_info | |
OmniAuth::Utils.deep_merge(super, { | |
'uid' => ui['uid'], | |
'user_info' => ui, | |
'extra' => {'user_hash' => user_hash} | |
}) | |
end | |
def user_info | |
entry = user_hash['entry'] | |
{ | |
'uid' => entry['id'], | |
'image' => entry['thumbnailUrl'], | |
} | |
end | |
def callback_phase | |
puts request.params["uid"] # nil | |
puts request.params["username"] # nil | |
puts request.params["avatar"] # nil | |
puts request.params["token"] # nil | |
end | |
def user_hash | |
puts "======= userID ======" | |
puts @access_token['id'] # nil | |
puts @access_token['login'] # nil | |
@user_hash ||= MultiJson.decode(@access_token.get("http://www.orkut.com/social/rest/people/default/@self").body) | |
end | |
def request_phase | |
request_token = consumer.get_request_token({:oauth_callback => callback_url}, {:scope => 'http://orkut.gmodules.com/social/rest'}) | |
session['oauth'] ||= {} | |
session['oauth'][name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret} | |
r = Rack::Response.new | |
if request_token.callback_confirmed? | |
r.redirect(request_token.authorize_url) | |
else | |
r.redirect(request_token.authorize_url(:oauth_callback => callback_url)) | |
end | |
r.finish | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment