Skip to content

Commit

Permalink
updated consumer to use sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Rennie committed Sep 13, 2011
1 parent 6e63325 commit b985af3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
26 changes: 23 additions & 3 deletions consumer/consumer.rb
@@ -1,9 +1,13 @@
require 'sinatra'
require 'oauth2'
require 'json'
enable :sessions

def client
OAuth2::Client.new('6IFWs6UowWqbDa4u1LCbZBpXWs2c8WbnkloWd90u', 'CosAmQVJcXPCFX4nqynCH6p67Yv2znLkOT3KpsiO', :site => 'http://localhost:3000')
end



get '/auth/test' do
redirect client.web_server.authorize_url(
Expand All @@ -13,14 +17,30 @@ def client

get '/auth/test/callback' do
access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
p access_token
response = access_token.get('/api/v1/data.json')
response.inspect
session[:access_token] = access_token.token
@message = JSON.parse(access_token.get('/api/v1/data.json'))
erb :success
end

get '/another_page' do
@message = get_response('data.json')
erb :different
end

get '/a_different_page' do
@message = get_response('data.json')
erb :success
end

def get_response(url)
access_token = OAuth2::AccessToken.new(client, session[:access_token])
JSON.parse(access_token.get("/api/v1/#{url}"))
end

def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/test/callback'
uri.query = nil
uri.to_s
end

5 changes: 5 additions & 0 deletions consumer/views/different.erb
@@ -0,0 +1,5 @@
<h1>A different page</h1>

<%=@message.inspect%>

<a href="/a_different_page">Check session is working</a>
5 changes: 5 additions & 0 deletions consumer/views/success.erb
@@ -0,0 +1,5 @@
<h1>A page</h1>

<%=@message.inspect%>

<a href="/another_page">Check session is working in a different page</a>
30 changes: 26 additions & 4 deletions readme.md
Expand Up @@ -148,10 +148,14 @@ Copy the following code, replacing the API keys from those of the client:

require 'sinatra'
require 'oauth2'
require 'json'
enable :sessions
def client
OAuth2::Client.new(consumer_key, consumer_secret, :site => 'http://localhost:3000')
OAuth2::Client.new('6IFWs6UowWqbDa4u1LCbZBpXWs2c8WbnkloWd90u', 'CosAmQVJcXPCFX4nqynCH6p67Yv2znLkOT3KpsiO', :site => 'http://localhost:3000')
end

get '/auth/test' do
redirect client.web_server.authorize_url(
Expand All @@ -161,15 +165,33 @@ Copy the following code, replacing the API keys from those of the client:
get '/auth/test/callback' do
access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
p access_token
response = access_token.get('/api/v1/data.json')
response.inspect
session[:access_token] = access_token.token
@message = JSON.parse(access_token.get('/api/v1/data.json'))
erb :success
end

get '/another_page' do
@message = get_response('data.json')
erb :different
end

get '/a_different_page' do
@message = get_response('data.json')
erb :success
end
def get_response(url)
access_token = OAuth2::AccessToken.new(client, session[:access_token])
JSON.parse(access_token.get("/api/v1/#{url}"))
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/test/callback'
uri.query = nil
uri.to_s
end

You can grab the required views from *consumer/views*


0 comments on commit b985af3

Please sign in to comment.