-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathoauth_v2.rb
More file actions
executable file
·60 lines (48 loc) · 1.33 KB
/
Copy pathoauth_v2.rb
File metadata and controls
executable file
·60 lines (48 loc) · 1.33 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'dotenv/load'
require 'webrick'
require 'slack-ruby-client'
require 'active_support'
require 'active_support/core_ext/object/to_query'
server = WEBrick::HTTPServer.new(Port: ENV['PORT'] || 4242)
trap 'INT' do
server.shutdown
end
server.mount_proc '/' do |req, res|
client = Slack::Web::Client.new
response = client.oauth_v2_access(
req.query.merge(
client_id: ENV['SLACK_CLIENT_ID'],
client_secret: ENV['SLACK_CLIENT_SECRET'],
grant_type: 'authorization_code'
)
)
pp response
res.body = %(
<html>
<body>
<ul>
<li>bot access_token: #{response.access_token}</li>
<li>token_type: #{response.token_type}</li>
<li>app_id: #{response.app_id}</li>
<li>scope: #{response.scope}</li>
<li>user: #{response.authed_user.id}</li>
<li>user access token: #{response.authed_user.access_token}</li>
</ul>
<body>
</html>
)
pp Slack::Web::Client.new(token: response.authed_user.access_token || response.access_token).auth_test
server.shutdown
end
query = {
client_id: ENV['SLACK_CLIENT_ID'],
redirect_uri: ENV['REDIRECT_URI'],
scope: ENV['SCOPE'],
user_scope: ENV['USER_SCOPE']
}
url = "https://slack.com/oauth/v2/authorize?#{query.to_query}"
puts "Opening browser at #{url}."
system 'open', url
server.start