Permalink
Browse files

refactor for oauth2-0.5

  • Loading branch information...
1 parent 9931a95 commit a59bc8a5f316df3111eaa2efcbdbe1a30c43f113 @mislav committed Sep 25, 2011
Showing with 31 additions and 21 deletions.
  1. +25 −15 lib/facebook/client.rb
  2. +6 −6 lib/facebook/login.rb
@@ -4,37 +4,47 @@ module Facebook
# http://www.facebook.com/developers/apps.php?app_id=APPID
class Client
class << self
- attr_accessor :app_id, :secret
+ attr_reader :last_oauth_client
end
-
- def self.oauth_client(app_id = nil, secret = nil)
- self.app_id, self.secret = app_id, secret unless app_id.nil?
- OAuth2::Client.new(self.app_id, self.secret, :site => 'https://graph.facebook.com')
+
+ def self.oauth_client(app_id, secret, &block)
+ @last_oauth_client = OAuth2::Client.new app_id, secret,
+ :site => 'https://graph.facebook.com',
+ :token_url => '/oauth/access_token',
+ &block
+ end
+
+ def self.access_token_options
+ {:mode => :query, :param_name => 'access_token'}
+ end
+
+ def self.restore_access_token(token_string)
+ OAuth2::AccessToken.new(last_oauth_client, token_string, access_token_options)
end
-
- def initialize(app_id, secret, options = {})
- @oauth = self.class.oauth_client(app_id, secret)
+
+ attr_reader :oauth
+
+ def initialize(app_id, secret, options = {}, &block)
+ @oauth = self.class.oauth_client(app_id, secret, &block)
@default_params = { :scope => options[:permissions], :display => options[:display] }
@user_fields = Array(options[:user_fields])
end
# params: redirect_uri, scope, display
def authorize_url(params = {})
- @oauth.web_server.authorize_url(@default_params.merge(params))
+ @oauth.auth_code.authorize_url(@default_params.merge(params))
end
def get_access_token(code, redirect_uri)
- @oauth.web_server.get_access_token(code, :redirect_uri => redirect_uri)
- end
-
- def restore_access_token(token_string)
- OAuth2::AccessToken.new(@oauth, token_string)
+ token = @oauth.auth_code.get_token(code, :redirect_uri => redirect_uri, :parse => :query)
+ token.options.update(self.class.access_token_options)
+ token
end
def login_handler(options = {})
Login.new(self, options)
end
-
+
def get_user_info(access_token, path)
access_token.get(path, :fields => @user_fields.join(','))
end
View
@@ -1,5 +1,4 @@
require 'facebook/client'
-require 'yajl'
require 'rack/request'
require 'addressable/uri'
@@ -28,11 +27,11 @@ def call(env)
module Helpers
def facebook_client
- OAuth2::AccessToken.new(facebook_oauth, session[:facebook_access_token])
+ Facebook::Client.restore_access_token session[:facebook_access_token]
end
def facebook_oauth
- Facebook::Client.oauth_client
+ Facebook::Client.last_oauth_client
end
def facebook_user
@@ -71,13 +70,14 @@ def url_for(path)
private
def handle_facebook_authorization(code, callback_url, request)
+ require 'json' unless defined? ::JSON
access_token = @client.get_access_token(code, callback_url)
user_info = @client.get_user_info(access_token, '/me')
-
+
request.session[:facebook_access_token] = access_token.token
- request.session[:facebook_user] = Yajl::Parser.parse(user_info)
+ request.session[:facebook_user] = JSON.parse(user_info.body)
redirect_to_return_path(request)
- end
+ end
def handle_error(error, request)
request.session[:facebook_error] = error

0 comments on commit a59bc8a

Please sign in to comment.