Class: RFacebook::FacebookDesktopSession

Inherits:
FacebookSession show all
Defined in:
lib/facebook_desktop_session.rb

Instance Attribute Summary collapse

Attributes inherited from FacebookSession

#logger, #session_expires, #session_key, #session_user_id

Instance Method Summary collapse

Methods inherited from FacebookSession

#_dump, _load, #expired?, #is_activated?, #is_expired?, #is_ready?, #is_valid?, #last_error_code, #last_error_message, #quiet=, #quiet?, #suppress_errors, #suppress_errors=

Constructor Details

#initialize(api_key, api_secret, quiet = false) ⇒ FacebookDesktopSession

Constructs a FacebookDesktopSession, calling the API to grab an auth_token.

api_key

your API key

api_secret

your API secret

quiet

boolean, set to true if you don’t want errors to be thrown (defaults to false)



44
45
46
47
48
49
# File 'lib/facebook_desktop_session.rb', line 44

def initialize(api_key, api_secret, quiet = false)
  super(api_key, api_secret, quiet)
  result = remote_call("auth.createToken", {})
  @desktop_auth_token = result.at("auth_createToken_response")
  @desktop_auth_token = @desktop_auth_token.nil? ? nil : @desktop_auth_token.inner_html.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RFacebook::FacebookSession

Instance Attribute Details

#session_secretObject (readonly)

you should need this for infinite desktop sessions



37
38
39
# File 'lib/facebook_desktop_session.rb', line 37

def session_secret
  @session_secret
end

Instance Method Details

#activateObject

Activates the session and makes it ready for usage. Call this method only after the user has logged in via the login URL.



76
77
78
79
80
81
82
83
# File 'lib/facebook_desktop_session.rb', line 76

def activate
  result = remote_call("auth.getSession", {:auth_token => @desktop_auth_token}, true)
  if result != nil
    @session_user_id = result.at("uid").inner_html
    @session_key = result.at("session_key").inner_html
    @session_secret = result.at("secret").inner_html
  end
end

#activate_with_previous_session(key, secret) ⇒ Object

Activate using the session key and secret directly (for example, if you have an infinite session)

key

the session key to use

secret

the session secret to use



89
90
91
92
93
94
95
96
97
# File 'lib/facebook_desktop_session.rb', line 89

def activate_with_previous_session(key, secret)
  # set the session key and secret
  @session_key = key
  @session_secret = secret

  # determine the current user's id
  result = remote_call("users.getLoggedInUser")
  @session_user_id = result.at("users_getLoggedInUser_response").inner_html
end

#get_login_url(options = {}) ⇒ Object

Gets the authentication URL

options.next

the page to redirect to after login

options.popup

boolean, whether or not to use the popup style (defaults to true)

options.skipcookie

boolean, whether to force new Facebook login (defaults to false)

options.hidecheckbox

boolean, whether to show the “infinite session” option checkbox (defaults to false)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/facebook_desktop_session.rb', line 57

def (options={})
  # options
  path_next = options[:next] ||= nil
  popup = (options[:popup] == nil) ? true : false
  skipcookie = (options[:skipcookie] == nil) ? false : true
  hidecheckbox = (options[:hidecheckbox] == nil) ? false : true

  # get some extra portions of the URL
  optionalNext = (path_next == nil) ? "" : "&next=#{CGI.escape(path_next.to_s)}"
  optionalPopup = (popup == true) ? "&popup=true" : ""
  optionalSkipCookie = (skipcookie == true) ? "&skipcookie=true" : ""
  optionalHideCheckbox = (hidecheckbox == true) ? "&hide_checkbox=true" : ""

  # build and return URL
  return "http://#{WWW_HOST}#{WWW_PATH_LOGIN}?v=1.0&api_key=#{@api_key}&auth_token=#{@desktop_auth_token}#{optionalPopup}#{optionalNext}#{optionalSkipCookie}#{optionalHideCheckbox}"
end

#ready?Boolean

returns true if this session is completely ready to be used and make API calls

Returns:

  • (Boolean)


100
101
102
# File 'lib/facebook_desktop_session.rb', line 100

def ready?
  return (@session_key != nil and @session_secret != nil and !expired?)
end

#signature(params) ⇒ Object

Used for signing a set of parameters in the way that Facebook specifies: <developers.facebook.com/documentation.php?v=1.0&doc=auth>

params

a Hash containing the parameters to sign



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/facebook_desktop_session.rb', line 108

def signature(params)
  # choose the proper secret
  signatureSecret = nil
  unless (params[:method] == "facebook.auth.getSession" or params[:method] == "facebook.auth.createToken")
    signatureSecret = @session_secret
  else
    signatureSecret = @api_secret
  end

  # sign the parameters with that secret
  return signature_helper(params, signatureSecret)
end