forked from Gazler/Oauth2-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
50 lines (40 loc) · 1.15 KB
/
app.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
require 'sinatra'
require 'oauth2'
require 'json'
enable :sessions
def client
# OAuth2::Client.new("KEY", "SECRET", :site => "http://localhost:3000")
OAuth2::Client.new("tV5Gks6HjhFskIL4j2I7NcPh85fCuplVjYQn6AEB", "R69Jcu2d8877YstLyd6DwvW7pxNSzlTLzyogZ7ux", :site => "http://localhost:3000")
end
get "/auth/test" do
redirect client.auth_code.authorize_url(:redirect_uri => redirect_uri)
end
get '/auth/test/callback' do
access_token = client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri)
session[:access_token] = access_token.token
@message = "Successfully authenticated with the server"
erb :success
end
get /\/test\/(.*)/ do |path|
p path
@message = get_response("#{path}.json")
erb :another
end
get '/yet_another' do
@message = get_response('data.json')
erb :success
end
get '/another_page' do
@message = get_response('contacts.json')
erb :another
end
def get_response(url)
access_token = OAuth2::AccessToken.new(client, session[:access_token])
JSON.parse(access_token.get("/api/v1/#{url}").body)
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/test/callback'
uri.query = nil
uri.to_s
end