-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoauth_example_app.rb
48 lines (35 loc) · 1.1 KB
/
oauth_example_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
require 'sinatra/base'
require 'rubygems'
require 'json'
require 'dailymile'
require 'erb'
Dailymile::Client.set_client_credentials ENV['CLIENT_ID'], ENV['CLIENT_SECRET']
class OauthExampleApp < Sinatra::Base
set :sessions, true
get '/' do
redirect '/authorize' unless session[:access_token]
@user = client.get '/people/me'
erb :index
end
get '/authorize' do
redirect Dailymile::Client.oauth_client.web_server.authorize_url(:redirect_uri => redirect_uri, :response_type => 'code')
end
get '/callback' do
begin
access_token = Dailymile::Client.oauth_client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri, :grant_type => 'authorization_code')
session[:access_token] = access_token.token
redirect '/'
rescue
%{Failed to retrieve access. <a href="/">Try again</a>}
end
end
private
def redirect_uri
uri = URI.parse(request.url)
"#{uri.scheme}://#{uri.host}:#{uri.port}/callback"
end
def client
@client ||= Dailymile::Client.new(session[:access_token])
end
end
OauthExampleApp.run!