Class: Mogli::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/mogli/authenticator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, secret, callback_url) ⇒ Authenticator

Returns a new instance of Authenticator.



8
9
10
11
12
# File 'lib/mogli/authenticator.rb', line 8

def initialize(client_id,secret,callback_url)
  @client_id = client_id
  @secret = secret
  @callback_url = callback_url
end

Instance Attribute Details

#callback_urlObject (readonly)

Returns the value of attribute callback_url.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def callback_url
  @callback_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def client_id
  @client_id
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def secret
  @secret
end

Instance Method Details

#access_token_url(code) ⇒ Object



19
20
21
# File 'lib/mogli/authenticator.rb', line 19

def access_token_url(code)
  "https://graph.facebook.com/oauth/access_token?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url) unless callback_url.nil? || callback_url.empty?}&client_secret=#{secret}&code=#{CGI.escape(code)}"
end

#authorize_url(options = {}) ⇒ Object



14
15
16
17
# File 'lib/mogli/authenticator.rb', line 14

def authorize_url(options = {})
  options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
  "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}#{options_part}"
end

#extend_access_token(oauth_token) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/mogli/authenticator.rb', line 23

def extend_access_token(oauth_token)
  response = Mogli::Client.get("https://graph.facebook.com/oauth/access_token",
    :query=>{
      :client_id=>@client_id,
      :client_secret => @secret,
      :grant_type => "fb_exchange_token",
      :fb_exchange_token=>oauth_token})
  raise_exception_if_required(response)
  Hash[response.parsed_response.split("&").map {|a| a.split("=")}]
end

#get_access_token_for_applicationObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mogli/authenticator.rb', line 51

def get_access_token_for_application
  client = Mogli::Client.new
  response = client.class.post(client.api_path('oauth/access_token'),
    :body=> {
      :grant_type => 'client_credentials',
      :client_id => client_id,
      :client_secret => secret
    }
  )
  raise_exception_if_required(response)
  response.parsed_response.split("=").last
end

#get_access_token_for_session_key(session_keys) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mogli/authenticator.rb', line 34

def get_access_token_for_session_key(session_keys)
  keystr = session_keys.is_a?(Array) ?
             session_keys.join(',') : session_keys
  client = Mogli::Client.new
  response = client.class.post(client.api_path("oauth/exchange_sessions"),
    :body => {
      :type => 'client_cred',
      :client_id => client_id,
      :client_secret => secret,
      :sessions => keystr
    }
  )
  raise_exception_if_required(response)
  tokens = response.parsed_response
  session_keys.is_a?(Array) ? tokens : tokens.first
end

#raise_exception_if_required(response) ⇒ Object



64
65
66
# File 'lib/mogli/authenticator.rb', line 64

def raise_exception_if_required(response)
  raise Mogli::Client::HTTPException, response.body if response.code != 200
end