lib/fb/auth.rb in fb-auth-0.1.3 vs lib/fb/auth.rb in fb-auth-1.0.0.alpha1
- old
+ new
@@ -1,12 +1,9 @@
-require 'fb/configuration'
-require 'fb/user'
-require 'fb/page'
-require 'fb/metric'
-require 'fb/request'
-# Ruby client to authenticate a Facebook user.
-# @see http://www.rubydoc.info/gems/Fb/
+require 'fb/support'
+
+# An object-oriented Ruby client for the Facebook Graph API.
+# @see http://www.rubydoc.info/gems/fb-core/
module Fb
# Provides methods to authenticate a user with the Facebook OAuth flow.
# @see https://developers.facebook.com/docs/facebook-login
class Auth
# @param [Hash] options the options to initialize an instance of Fb::Auth.
@@ -17,47 +14,47 @@
def initialize(options = {})
@redirect_uri = options[:redirect_uri]
@code = options[:code]
end
- # @return [String] a url to Facebook's account authentication.
+ # @return [String] the url to authenticate as a Facebook user.
def url
- Fb::Request.new(url_options).url
+ HTTPRequest.new(url_options).url
end
- # @return [String] the non-expiring access token of an authenticated Facebook account.
+ # @return [String] the non-expiring access token of a Facebook user.
def access_token
- response_body = Fb::Request.new(path: '/oauth/access_token',
- params: long_term_token_params).run
- response_body["access_token"]
+ params = {redirect_uri: @redirect_uri, code: @code}
+ temp_token = fetch_access_token_with params
+
+ params = {grant_type: :fb_exchange_token, fb_exchange_token: temp_token}
+ fetch_access_token_with params
end
private
- def short_term_access_token
- response_body = Fb::Request.new(path: '/oauth/access_token',
- params: short_term_token_params).run
- response_body["access_token"]
- end
-
def url_options
- url_params = {scope: 'email,manage_pages', redirect_uri: @redirect_uri}
{host: 'www.facebook.com', path: '/dialog/oauth', params: url_params}
end
- def short_term_token_params
+ def url_params
{}.tap do |params|
- params[:client_secret] = Fb.configuration.client_secret
+ params[:client_id] = Fb.configuration.client_id
params[:redirect_uri] = @redirect_uri
- params[:code] = @code
+ params[:scope] = 'email,manage_pages,read_insights'
end
end
- def long_term_token_params
+ def fetch_access_token_with(params)
+ params = params.merge access_token_params
+ request = HTTPRequest.new path: '/oauth/access_token', params: params
+ request.run.body['access_token']
+ end
+
+ def access_token_params
{}.tap do |params|
+ params[:client_id] = Fb.configuration.client_id
params[:client_secret] = Fb.configuration.client_secret
- params[:grant_type] = :fb_exchange_token
- params[:fb_exchange_token] = short_term_access_token
end
end
end
end