Method: FBCLI.login

Defined in:
lib/fbcli/auth.rb

.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.(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"