Skip to content

Commit 688304f

Browse files
authored
Merge pull request #105 from jcraigk/oauth-readme
Update README with OAuth code grant detail
2 parents 46a2e81 + 304cec3 commit 688304f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,45 @@ See the [sample app using ActiveRecord](sample_apps/sample_app_activerecord) for
6262

6363
### Usage
6464

65-
Start with one of the samples above, which contain a couple of custom commands, necessary dependencies and tests, then [create a new application](https://api.slack.com/applications/new) on Slack.
65+
Start with one of the samples above, which contain a couple of custom commands, necessary dependencies and tests, then [create a new Slack App](https://api.slack.com/applications/new).
6666

67-
![](images/new.png)
67+
![](images/create-app.png)
6868

69-
Follow Slack's instructions, note the app client ID and secret, give the bot a default name, etc. The redirect URL should be the location of your app, for local testing purposes use `http://localhost:9292`.
69+
Follow Slack's instructions, note the app client ID and secret, give the bot a default name, etc. The redirect URL should be the location of your app. For local testing purposes use a public tunneling service such as [ngrok](https://ngrok.com/) to expose local port 9292.
7070

7171
Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
7272

7373
Run `bundle install` and `foreman start` to boot the app. Navigate to [localhost:9292](http://localhost:9292). You should see an "Add to Slack" button. Use it to install the app into your own Slack team.
7474

75+
### OAuth Code Grant
76+
77+
The "Add to Slack" button uses the standard OAuth code grant flow as described in the [Slack docs](https://api.slack.com/docs/oauth#flow).
78+
79+
The button itself contains a link that looks like this:
80+
81+
```
82+
https://slack.com/oauth/authorize?scope=bot&client_id=<%= ENV['SLACK_CLIENT_ID'] %>
83+
```
84+
85+
Once clicked, the user is taken through the authorization process at Slack's site. Upon successful completion, a callback containing a temporary code is sent to the redirect URL you specified. The endpoint at that URL contains code that looks like this:
86+
87+
```ruby
88+
# Instantiate a web client
89+
client = Slack::Web::Client.new
90+
91+
# Request a token using the temporary code
92+
rc = client.oauth_access(
93+
client_id: ENV['SLACK_CLIENT_ID'],
94+
client_secret: ENV['SLACK_CLIENT_SECRET'],
95+
code: params[:code]
96+
)
97+
98+
# Pluck the token from the response
99+
token = rc['bot']['bot_access_token']
100+
```
101+
102+
The token is stored in persistent storage and used each time a Slack client is instantiated for the specific team.
103+
75104
### API
76105

77106
This library implements an app, [SlackRubyBotServer::App](lib/slack-ruby-bot-server/app.rb), a service manager, [SlackRubyBotServer::Service](lib/slack-ruby-bot-server/service.rb) that creates multiple instances of a bot server class, [SlackRubyBotServer::Server](lib/slack-ruby-bot-server/server.rb), one per team.

images/create-app.png

85 KB
Loading

images/new.png

-138 KB
Binary file not shown.

0 commit comments

Comments
 (0)