-
Notifications
You must be signed in to change notification settings - Fork 9
/
facebook-oauth-example.rb
64 lines (48 loc) · 1.46 KB
/
facebook-oauth-example.rb
1
2
3
4
5
6
7
8
9
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
require "rubygems"
require "sinatra"
require "net/http"
require "net/https"
require "cgi"
require "json"
enable :sessions
before do
@client_id = "YOUR-CLIENT-ID-HERE"
@client_secret = "YOUR-CLIENT-SECRET-HERE"
session[:oauth] ||= {}
end
get "/" do
if session[:oauth][:access_token].nil?
erb :start
else
http = Net::HTTP.new "graph.facebook.com", 443
request = Net::HTTP::Get.new "/me?access_token=#{session[:oauth][:access_token]}"
http.use_ssl = true
response = http.request request
@json = JSON.parse(response.body)
erb :ready
end
end
get "/request" do
redirect "https://graph.facebook.com/oauth/authorize?client_id=#{@client_id}&redirect_uri=http://localhost:4567/callback"
end
get "/callback" do
session[:oauth][:code] = params[:code]
http = Net::HTTP.new "graph.facebook.com", 443
request = Net::HTTP::Get.new "/oauth/access_token?client_id=#{@client_id}&redirect_uri=http://localhost:4567/callback&client_secret=#{@client_secret}&code=#{session[:oauth][:code]}"
http.use_ssl = true
response = http.request request
session[:oauth][:access_token] = CGI.parse(response.body)["access_token"][0]
redirect "/"
end
get "/logout" do
session[:oauth] = {}
redirect "/"
end
enable :inline_templates
__END__
@@ start
<a href="/request">Let's see who you are</a>.
@@ ready
<img style="padding: 20px" src="http://graph.facebook.com/<%= @json["id"] %>/picture" />
<br />
Hello, <%= @json["name"] %>! <a href="/logout">Logout</a>.