/
github-v3-api-get-token
executable file
·46 lines (35 loc) · 1.11 KB
/
github-v3-api-get-token
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
puts <<EOF
This script will start a sinatra web app on your machine that can log you in to
GitHub and show you your access token. This access token can then be used with
the GitHubV3API. You will need to set the OAUTH_GITHUB_CLIENT_ID and
OAUTH_GITHUB_CLIENT_SECRET environment variables. See the README for more
information.
EOF
print "Ready to go? (y/N): "
answer = STDIN.gets.chomp
unless answer =~ /^y(es)?$/i
exit(1)
end
unless ENV['OAUTH_GITHUB_CLIENT_ID'] && ENV['OAUTH_GITHUB_CLIENT_SECRET']
raise "Please set the OAUTH_GITHUB_CLIENT_ID and OAUTH_GITHUB_CLIENT_SECRET " \
+ "environment variables before running this."
end
require 'rubygems'
require 'bundler/setup'
Bundler.setup(:sinatra)
require 'sinatra'
require 'openssl'
require 'omniauth'
enable :sessions
use OmniAuth::Builder do
provider :github, ENV['OAUTH_GITHUB_CLIENT_ID'], ENV['OAUTH_GITHUB_CLIENT_SECRET'],
:scope => 'user,repo,gist'
end
get '/' do
redirect '/auth/github'
end
get '/auth/github/callback' do
token = request.env['omniauth.auth']['credentials']['token']
"Your GitHub Access Token is #{token}"
end