Class: Fobos::GraphAPI::OAuth

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fobos/graph_api/o_auth.rb

Constant Summary collapse

FB_URI =

Store Facebook default URL

'https://www.facebook.com'
GRAPH_URI =

Store Facebook Graph API URL

'https://graph.facebook.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_secret, oauth_callback_url) ⇒ OAuth

Initialize new object for Facebook OAuth actions.



32
33
34
35
36
# File 'lib/fobos/graph_api/o_auth.rb', line 32

def initialize(app_id, app_secret, oauth_callback_url)
  @app_id = app_id
  @app_secret = app_secret
  @oauth_callback_url = oauth_callback_url
end

Instance Attribute Details

#access_tokenObject

Store long-live access token (set it manually).



24
25
26
# File 'lib/fobos/graph_api/o_auth.rb', line 24

def access_token
  @access_token
end

#app_idObject

ID of your Facebook App. You can get in your app’s manage page.



18
19
20
# File 'lib/fobos/graph_api/o_auth.rb', line 18

def app_id
  @app_id
end

#app_secretObject

Secret key of your Facebook App. You can get in your app’s manage page.



20
21
22
# File 'lib/fobos/graph_api/o_auth.rb', line 20

def app_secret
  @app_secret
end

#oauth_callback_urlObject

URL for redirecting after FB action on FB server-side. NOTE: callback_url must be the same for get_oauth_url and get_user_access_token_url



22
23
24
# File 'lib/fobos/graph_api/o_auth.rb', line 22

def oauth_callback_url
  @oauth_callback_url
end

Instance Method Details

#get_user_access_code_from_params(params) ⇒ Object

Return users access code from params which returned by Facebook



65
66
67
68
69
70
71
72
# File 'lib/fobos/graph_api/o_auth.rb', line 65

def get_user_access_code_from_params(params)
  code = params[:code]
  if code.nil?
    raise 'An error has occurred. You code is nil. Please check is your actions is valid.'
  else
    code
  end
end

#get_user_access_code_url(oauth_callback_url = @oauth_callback_url, options = {}) ⇒ Object

Generate link for getting user’s “access code” (NOT ACCESS TOKEN). After you make call of this link FB redirect you to @oauth_callback_url with “code” in params.

Options you can see here developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.1#login



41
42
43
44
45
46
47
48
# File 'lib/fobos/graph_api/o_auth.rb', line 41

def get_user_access_code_url(oauth_callback_url = @oauth_callback_url, options = {})
  options_part = ''
  options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?

  query = "/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{oauth_callback_url}#{options_part}"

  (FB_URI.to_s + query.to_s).to_s
end

#get_user_access_token(oauth_callback_url = @oauth_callback_url, code) ⇒ Object

Provide call of link what is result of get_user_access_token_url. Returned parsed access_token.



58
59
60
61
62
# File 'lib/fobos/graph_api/o_auth.rb', line 58

def get_user_access_token(oauth_callback_url = @oauth_callback_url, code)
  response = self.class.get(get_user_access_token_url(oauth_callback_url, code))
  parsed_params = CGI::parse(response.parsed_response)
  parsed_params["access_token"].first
end

#get_user_access_token_url(oauth_callback_url = @oauth_callback_url, code) ⇒ Object

Generate link for getting user’s ACCESS TOKEN by code given with get_user_access_code



51
52
53
54
55
# File 'lib/fobos/graph_api/o_auth.rb', line 51

def get_user_access_token_url(oauth_callback_url = @oauth_callback_url, code)
  query = "/oauth/access_token?client_id=#{@app_id}&client_secret=#{@app_secret}&code=#{code}&redirect_uri=#{oauth_callback_url}"

  GRAPH_URI.to_s + query.to_s
end

#get_user_long_live_access_token(access_token) ⇒ Object

Provide call of link what is result of get_user_long_live_access_token_url



82
83
84
# File 'lib/fobos/graph_api/o_auth.rb', line 82

def get_user_long_live_access_token(access_token)
  self.class.get(get_user_long_live_access_token_url(access_token))
end

#get_user_long_live_access_token_url(access_token) ⇒ Object

Generate link for getting long-live token (~60 days) instead short-live token (~1-2 hours)



75
76
77
78
79
# File 'lib/fobos/graph_api/o_auth.rb', line 75

def get_user_long_live_access_token_url(access_token)
  query = "/oauth/access_token?grant_type=fb_exchange_token&client_id=#{app_id}&client_secret=#{app_secret}&fb_exchange_token=#{access_token}"

  GRAPH_URI.to_s + query.to_s
end