Module: FBCLI
- Defined in:
- lib/fbcli/api.rb,
lib/fbcli/auth.rb,
lib/fbcli/version.rb
Constant Summary collapse
- API_VERSION =
"3.1"- VERSION =
'1.7.2'- @@api =
nil
Class Method Summary collapse
- .api_call(lambda) ⇒ Object
- .get_access_token(app_id, auth_code, app_secret, redirect_uri) ⇒ Object
- .init_api ⇒ Object
- .koala_error_str(e) ⇒ Object
- .login(app_id, app_secret, local_host, local_port) ⇒ Object
- .logout ⇒ Object
- .page_items(cmd, separator = nil, filter = nil) ⇒ Object
- .raw_request(req) ⇒ Object
- .request_object(id, options = {}) ⇒ Object
- .request_personal_connections(cmd) ⇒ Object
- .request_token_info(access_token = $config['access_token']) ⇒ Object
Class Method Details
.api_call(lambda) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/fbcli/api.rb', line 29 def self.api_call(lambda) @@api = init_api if @@api.nil? begin lambda.call(@@api) rescue Koala::Facebook::APIError => e exit_now! koala_error_str e end end |
.get_access_token(app_id, auth_code, app_secret, redirect_uri) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fbcli/auth.rb', line 71 def self.get_access_token(app_id, auth_code, app_secret, redirect_uri) # The redirect_uri doesn't play the same role as in capturing the auth code, however # it must match the one used in the previous case, otherwise the server will reject # the request. # # See: https://www.oauth.com/oauth2-servers/access-tokens/authorization-code-request/ auth_uri = "https://graph.facebook.com/v#{API_VERSION}/oauth/access_token?" + "client_id=#{app_id}" + "&redirect_uri=#{redirect_uri}" + "&client_secret=#{app_secret}" + "&code=#{auth_code}" res = Net::HTTP.get_response(URI.parse(auth_uri)) res = JSON.parse(res.body) res["access_token"] end |
.init_api ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/fbcli/api.rb', line 6 def self.init_api if $config['access_token'].nil? or $config['access_token'].empty? exit_now! "Obtain an access token to interact with the Facebook API.\n\nRun: \#{APP_NAME} login\n EOM\n end\n\n Koala::Facebook::API.new($config['access_token'])\nend\n" |
.koala_error_str(e) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fbcli/api.rb', line 18 def self.koala_error_str(e) str = "Koala #{e.fb_error_type}" str << " (code #{e.fb_error_code.to_s + (e.fb_error_subcode.nil? ? "" : ", subcode: " + e.fb_error_subcode.to_s)})" str << " HTTP status: #{e.http_status}" unless e.http_status.nil? str << "\n #{e.fb_error_user_msg.nil? ? e.fb_error_message : e.fb_error_user_msg}" str << " (FB trace id: #{e.fb_error_trace_id})" str end |
.login(app_id, app_secret, local_host, local_port) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fbcli/auth.rb', line 10 def self.login(app_id, app_secret, local_host, local_port) redirect_uri = "https://#{local_host}:#{local_port}/" uri = "https://www.facebook.com/dialog/oauth?client_id=#{app_id}" + "&redirect_uri=#{redirect_uri}" + "&scope=user_likes,user_posts,user_photos,user_videos" puts "Open this URL in a web browser and allow access to the Facebook Graph on behalf of your user account:\n\n\#{uri}\n\nWaiting to receive authorization code on port \#{local_port}...\n\n EOM\n\n server = WEBrick::HTTPServer.new(\n :Port => local_port,\n :SSLEnable => true,\n :SSLCertName => [ %w[CN localhost] ],\n :Logger => WEBrick::Log.new(File.open(File::NULL, 'w')),\n :AccessLog => []\n )\n\n # TODO: WEBrick generates a self-signed SSL certificate with serial number 1, which\n # causes Firefox to complain with SEC_ERROR_REUSED_ISSUER_AND_SERIAL on the\n # second authentication attempt.\n #\n # See: https://github.com/ruby/webrick/blob/6e9081b/lib/webrick/ssl.rb#L112\n #\n # Providing a self-signed certificate to WEBrick during instantiation is one\n # way to surmount this inconvenience.\n\n access_token = nil\n\n server.mount_proc '/' do |req, res|\n key, value = req.query_string.split '=', 2\n\n if key == \"code\"\n puts \"Received authorization code. Exchanging it for an access token...\"\n puts\n\n access_token = get_access_token(app_id, value, app_secret, redirect_uri)\n else\n puts \"Received unexpected request: \#{req.query_string}\"\n end\n\n res.body = 'You may now close this window.'\n server.shutdown\n end\n\n # Allow CTRL+C intervention\n trap 'INT' do server.shutdown end\n\n # Block execution on this thread until server shuts down\n server.start\n\n # Return access token\n access_token\nend\n" |
.logout ⇒ Object
45 46 47 48 49 |
# File 'lib/fbcli/api.rb', line 45 def self.logout api_call lambda { |api| api.delete_object("me/permissions") } end |
.page_items(cmd, separator = nil, filter = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fbcli/api.rb', line 73 def self.page_items(cmd, separator = nil, filter = nil) items = request_personal_connections(cmd) virgin = true count = 0 while not (items.nil? or count == ['pages']) do items.each_with_index { |item, idx| if filter.nil? or not filter.call(item) unless separator.nil? or virgin puts separator end yield item virgin = false end } count += 1 items = items.next_page end end |
.raw_request(req) ⇒ Object
39 40 41 42 43 |
# File 'lib/fbcli/api.rb', line 39 def self.raw_request(req) api_call lambda { |api| api.graph_call req } end |
.request_object(id, options = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/fbcli/api.rb', line 59 def self.request_object(id, = {}) api_call lambda { |api| api.get_object(id, ) do |data| yield data end } end |
.request_personal_connections(cmd) ⇒ Object
67 68 69 70 71 |
# File 'lib/fbcli/api.rb', line 67 def self.request_personal_connections(cmd) api_call lambda { |api| api.get_connections("me", cmd) } end |
.request_token_info(access_token = $config['access_token']) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/fbcli/api.rb', line 51 def self.request_token_info(access_token = $config['access_token']) api_call lambda { |api| api.debug_token access_token do |res| yield res['data'] end } end |