Skip to content

Instantly share code, notes, and snippets.

@adammiribyan
Forked from amfeng/callback.erb
Created November 5, 2012 08:38
Show Gist options
  • Save adammiribyan/4016063 to your computer and use it in GitHub Desktop.
Save adammiribyan/4016063 to your computer and use it in GitHub Desktop.
Stripe OAuth Example -- Ruby
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
<%= @access_token %>
</body>
</html>
api_key: YOUR_SECRET_API_KEY
client_id: YOUR_CLIENT_ID
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
<a href="/authorize">Connect with Stripe</a>
</body>
</html>
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'yaml'
require 'json'
require 'stripe'
class Server < Sinatra::Base
configure do
config = YAML::load(File.open('config.yml'))
set :api_key, config['api_key']
set :client_id, config['client_id']
options = {
:site => 'https://connect.stripe.com',
:authorize_url => '/oauth/authorize',
:token_url => '/oauth/token'
}
set :client, OAuth2::Client.new(settings.client_id, '', options)
end
get '/' do
erb :index
end
get '/authorize' do
params = {
:scope => 'read_write'
}
# Redirect the user to the authorize_uri endpoint
url = settings.client.auth_code.authorize_url(params)
redirect url
end
get '/oauth/callback' do
# Pull the authorization_code out of the response
code = params[:code]
# Use Bearer Authorization with our secret live API key
params = {
:headers => {'Authorization' => "Bearer #{settings.api_key}"}
}
# Make a request to the access_token_uri endpoint to get an access_token
@resp = settings.client.auth_code.get_token(code, params)
@access_token = @resp.token
# Use the access_token as you would a regular live-mode API key
# TODO: Stripe logic
erb :callback
end
end
Server.run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment