Skip to content

Commit c60c6a1

Browse files
committed
Added OAuth v2 and files_upload_v2 samples.
1 parent 087b640 commit c60c6a1

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SLACK_API_TOKEN=

examples/files_upload_v2/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
source 'http://rubygems.org'
3+
4+
gem 'dotenv'
5+
gem 'slack-ruby-client', path: '../..'

examples/files_upload_v2/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Samples for using `files_upload_v2`.
2+
3+
## Usage
4+
5+
Create a Slack app and obtain a User OAuth Token [here](https://api.slack.com/tutorials/tracks/getting-a-token) or use the [oauth_v2](../oauth_v2) example to obtain a User OAuth Token.
6+
Make sure to select `files:write` and `files:read` scopes under "User Token Scopes".
7+
8+
Create `.env` file with `SLACK_API_TOKEN` or set the `SLACK_API_TOKEN` environment variable.
9+
10+
```
11+
bundle install
12+
bundle exec dotenv ruby files_upload_v2.rb
13+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
require 'slack-ruby-client'
3+
require 'securerandom'
4+
5+
Slack.configure do |config|
6+
config.token = ENV['SLACK_API_TOKEN']
7+
raise 'Missing ENV[SLACK_API_TOKEN]!' unless config.token
8+
end
9+
10+
client = Slack::Web::Client.new
11+
12+
auth = client.auth_test
13+
puts "Connected to team #{auth.team} (#{auth.team_id}) as #{auth.user}."
14+
15+
puts client.files_upload_v2(
16+
filename: 'files_upload_v2.txt',
17+
content: SecureRandom.hex
18+
).files.first.permalink_public
19+
20+
puts client.files_upload_v2(
21+
filename: 'files_upload_v2_to_general_channel.txt',
22+
content: SecureRandom.hex,
23+
channel: '#general'
24+
).files.first.permalink_public
25+
26+
puts client.files_upload_v2(
27+
filename: 'files_upload_v2_to_general_and_random_channels.txt',
28+
content: SecureRandom.hex,
29+
channels: ['#general', '#random']
30+
).files.first.permalink_public
31+
32+
channel_id = client.conversations_id(channel: '#general')['channel']['id']
33+
puts client.files_upload_v2(
34+
filename: 'files_upload_v2_to_general_by_id.txt',
35+
content: SecureRandom.hex,
36+
channel_id: channel_id
37+
).files.first.permalink_public

examples/oauth_v2/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SLACK_CLIENT_ID=
2+
SLACK_CLIENT_SECRET=
3+
REDIRECT_URI=https://...ngrok-free.app
4+
SCOPE=files:read,files:write,files:write:user

examples/oauth_v2/Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
source 'http://rubygems.org'
3+
4+
gem 'activesupport'
5+
gem 'dotenv'
6+
gem 'slack-ruby-client', path: '../..'
7+
gem 'webrick'

examples/oauth_v2/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Obtain a user OAuth token from Slack.
2+
3+
Create a Slack app [here](https://api.slack.com/tutorials/tracks/getting-a-token).
4+
5+
The new app has a pre-configured User OAuth Token which you can use for most of the examples.
6+
However you may want to obtain a token for a different user, which can be accomplished as follows.
7+
8+
To test locally use [ngrok](https://ngrok.com/) to expose your local server to the internet.
9+
10+
```sh
11+
ngrok http 4242
12+
```
13+
14+
Add the ngrok URL to the app's "Redirect URLs" under "OAuth & Permissions" in the app's settings.
15+
16+
Create a `.env` file with the following:
17+
18+
```
19+
SLACK_CLIENT_ID=[App Client ID]
20+
SLACK_CLIENT_SECRET=[App Client Secret]
21+
REDIRECT_URI=[Your ngrok URL, e.g. https://...ngrok-free.app]
22+
SCOPE=[Bot User OAuth Scopes Requested]
23+
USER_SCOPE=[User OAuth Token Scopes Requested]
24+
```
25+
26+
Run the example.
27+
28+
```sh
29+
bundle install
30+
bundle exec dotenv ruby oauth_v2.rb
31+
```
32+
33+
A browser window will open to complete the OAuth flow.

examples/oauth_v2/oauth_v2.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'dotenv/load'
5+
require 'webrick'
6+
require 'slack-ruby-client'
7+
require 'active_support'
8+
require 'active_support/core_ext/object/to_query'
9+
10+
server = WEBrick::HTTPServer.new(Port: ENV['PORT'] || 4242)
11+
12+
trap 'INT' do
13+
server.shutdown
14+
end
15+
16+
server.mount_proc '/' do |req, res|
17+
client = Slack::Web::Client.new
18+
19+
response = client.oauth_v2_access(
20+
req.query.merge(
21+
client_id: ENV['SLACK_CLIENT_ID'],
22+
client_secret: ENV['SLACK_CLIENT_SECRET'],
23+
grant_type: 'authorization_code'
24+
)
25+
)
26+
27+
pp response
28+
29+
res.body = %(
30+
<html>
31+
<body>
32+
<ul>
33+
<li>bot access_token: #{response.access_token}</li>
34+
<li>token_type: #{response.token_type}</li>
35+
<li>app_id: #{response.app_id}</li>
36+
<li>scope: #{response.scope}</li>
37+
<li>user: #{response.authed_user.id}</li>
38+
<li>user access token: #{response.authed_user.access_token}</li>
39+
</ul>
40+
<body>
41+
</html>
42+
)
43+
44+
pp Slack::Web::Client.new(token: response.authed_user.access_token || response.access_token).auth_test
45+
46+
server.shutdown
47+
end
48+
49+
query = {
50+
client_id: ENV['SLACK_CLIENT_ID'],
51+
redirect_uri: ENV['REDIRECT_URI'],
52+
scope: ENV['SCOPE'],
53+
user_scope: ENV['USER_SCOPE']
54+
}
55+
56+
url = "https://slack.com/oauth/v2/authorize?#{query.to_query}"
57+
puts "Opening browser at #{url}."
58+
system 'open', url
59+
60+
server.start

0 commit comments

Comments
 (0)