Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add an example for the OAuth web server flow

  • Loading branch information...
commit 68f62d904a7dae642c6b84bfd43f3fc392c82eff 1 parent b9b1258
@theflow theflow authored
Showing with 62 additions and 2 deletions.
  1. +7 −2 README.md
  2. +55 −0 examples/sinatra.rb
View
9 README.md
@@ -29,9 +29,14 @@ After the configuration you need to authenticate against the API. The client sup
### Web Server Flow
-The default OAuth flow to be used when you authenticate Podio users from your web application.
+The default OAuth flow to be used when you authenticate Podio users from your web application. See the `sinatra.rb` in the examples folder.
- Podio.client.authenticate_with_auth_code('AUTHORIZATION_CODE', redirect_uri)
+ # Redirect the user to the authorize url
+ Podio.client.authorize_url(:redirect_uri => redirect_uri)
+
+ # In the callback you get the authorization_code
+ # wich you use to get the access token
+ Podio.client.authenticate_with_auth_code(params[:code], redirect_uri)
### Username and Password Flow
View
55 examples/sinatra.rb
@@ -0,0 +1,55 @@
+#
+# Basic OAuth web server flow example
+#
+# You need the sinatra and podio gems installed
+# and provide your API key in the Podio.setup call
+#
+# Configure your API key on https://podio.com/settings/api
+# so that the return URL is 'localhost'
+#
+# Start with
+# ruby sinatra.rb
+#
+# And point your browser to http://localhost:4567
+#
+
+require 'rubygems'
+require 'sinatra'
+require 'podio'
+
+
+# CHANGE this this to make this example work
+Podio.setup(
+ :api_key => 'YOUR_API_KEY',
+ :api_secret => 'YOUR_API_SECRET'
+)
+
+get '/' do
+ %(<p>Update the <code>Podio.setup</code> call in the sinatra app and <a href="/auth/podio">try to authorize</a>.</p>)
+end
+
+# access this to request a token from Podio
+get '/auth/podio' do
+ redirect Podio.client.authorize_url(:redirect_uri => redirect_uri)
+end
+
+# If the user authorizes it, this request gets your authorization code
+# which is used to get an access token and make a successful api call
+get '/auth/podio/callback' do
+ begin
+ # normally you store the token in the session to be able
+ # to make API calls with it in subsequent requests
+ token = Podio.client.authenticate_with_auth_code(params[:code], redirect_uri)
+ user = Podio::UserStatus.current
+
+ "<p><b>Your OAuth access token</b>: #{token.access_token}</p><p><b>Your user status info</b>:\n#{user.inspect}</p>"
+ rescue Podio::BadRequestError
+ %(<p>Outdated authorization code:</p><p>#{$!}</p><p><a href="/auth/podio">Start over</a></p>)
+ end
+end
+
+def redirect_uri(path='/auth/podio/callback')
+ uri = URI.parse(request.url)
+ uri.path = path
+ uri.to_s
+end
Please sign in to comment.
Something went wrong with that request. Please try again.