Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created March 19, 2012 15:24
Show Gist options
  • Save swdyh/2116130 to your computer and use it in GitHub Desktop.
Save swdyh/2116130 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'net/https'
client_id = '2044a7773387915a7a2a'
client_secret = 'secret'
get '/' do
html = <<-EOS
<html>
<head>
<style>body { font-size: 200%; font-weight:bold; margin: 20% 0; text-align:center; }</style>
</head>
<body>
<a href ="https://github.com/login/oauth/authorize?client_id=#{client_id}&amp;scope=gist">Get Github access token for gisty</a>
</body>
</html>
EOS
end
get '/callback' do
url = URI.parse 'https://github.com/login/oauth/access_token'
req = Net::HTTP::Post.new url.path
req.set_form_data :code => params[:code], :client_id => client_id, :client_secret => client_secret
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.verify_depth = 5
res = https.start {|http| http.request(req) }
res.body
at = Rack::Utils.parse_query(res.body)['access_token']
halt 500 unless at
html = <<-EOS
<html>
<head>
<style>body { font-size: 200%; font-weight:bold; margin: 20% 0; text-align:center; } code { font-size: 70%; color: white; background-color: black; padding: 1em; margin: 1em;} </style>
</head>
<body>
Set your shell env.<br /><br />
<code>export GISTY_ACCESS_TOKEN=#{Rack::Utils.escape_html at}</code>
</body>
</html>
EOS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment