Last active
August 8, 2022 19:33
-
-
Save amfeng/2314782 to your computer and use it in GitHub Desktop.
Stripe OAuth Example -- Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<head> | |
<title>Stripe OAuth Example</title> | |
</head> | |
<body> | |
<%= @access_token %> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api_key: YOUR_SECRET_API_KEY | |
client_id: YOUR_CLIENT_ID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<head> | |
<title>Stripe OAuth Example</title> | |
</head> | |
<body> | |
<a href="/authorize">Connect with Stripe</a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, settings.api_key, 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] | |
# Make a request to the access_token_uri endpoint to get an access_token | |
@resp = settings.client.auth_code.get_token(code, :params => {:scope => 'read_write'}) | |
@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
Here is how I pre-fill the user's info, let me know if there is a better way.