Skip to content

Commit 443ca03

Browse files
authored
docs: reference creating an app with scopes to gather a token for installation (#1694)
1 parent 3ab4638 commit 443ca03

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

docs/content/installation.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
This package supports Python 3.6 and higher. We recommend using [PyPI](https://pypi.python.org/pypi) to install. Run the following command:
44

5-
``` bash
5+
```bash
66
pip install slack_sdk
77
```
88

99
Alternatively, you can always pull the source code directly into your
1010
project:
1111

12-
``` bash
12+
```bash
1313
git clone https://github.com/slackapi/python-slack-sdk.git
1414
cd python-slack-sdk
1515
python3 -m venv .venv
@@ -20,7 +20,7 @@ pip install -e . # install the SDK project into the virtual env
2020

2121
Create a `./test.py` file with the following:
2222

23-
``` python title="test.py"
23+
```python title="test.py"
2424
# test.py
2525
import sys
2626
# Enable debug logging
@@ -34,14 +34,21 @@ api_response = client.api_test()
3434

3535
Then, run the script:
3636

37-
``` bash
37+
```bash
3838
python test.py
3939
```
4040

4141
It's also good to try on the Python REPL.
4242

4343
## Access Tokens {#handling-tokens}
4444

45+
Making calls to the Slack API often requires a [token](https://docs.slack.dev/authentication/tokens) with associated scopes that grant access to resources.
46+
47+
Collecting a token can be done from app settings or with an OAuth installation depending on your app's requirements:
48+
49+
- [Single Workspace Install](#single-workspace-install)
50+
- [Multiple Workspace Install](#multiple-workspace-install)
51+
4552
**Always keep your access tokens safe.**
4653

4754
The OAuth token you use to call the Slack API has access to the data on
@@ -52,12 +59,11 @@ ability to read and write data. Treat these tokens just as you would a
5259
password — don't publish them, don't check them into source code, and
5360
don't share them with others.
5461

55-
5662
:::danger
5763

5864
Never do the following:
5965

60-
``` python
66+
```python
6167
token = 'xoxb-111-222-xxxxx'
6268
```
6369

@@ -67,13 +73,13 @@ We recommend you pass tokens in as environment variables, or persist
6773
them in a database that is accessed at runtime. You can add a token to
6874
the environment by starting your app as:
6975

70-
``` python
76+
```python
7177
SLACK_BOT_TOKEN="xoxb-111-222-xxxxx" python myapp.py
7278
```
7379

7480
Then retrieve the key with:
7581

76-
``` python
82+
```python
7783
import os
7884
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
7985
```
@@ -85,13 +91,11 @@ Credentials](https://api.slack.com/authentication/best-practices) page within th
8591

8692
### Single Workspace Install
8793

88-
If you're building an application for a single Slack workspace,
89-
there's no need to build out the entire OAuth flow.
94+
If you're building an application for a single Slack workspace, there's no need to build out the entire OAuth flow.
95+
96+
After [creating an app](https://api.slack.com/apps?new_app=1) and adding [scopes](http://docs.slack.dev/reference/scopes) on the **OAuth & Permissions** page, go to the **Install App** page and click the **Install to Team** button to authorize the app and generate a token.
9097

91-
Once you've setup your features, click on the **Install App to Team**
92-
button found on the **Install App** page. If you add new permission
93-
scopes or Slack app features after an app has been installed, you must
94-
reinstall the app to your workspace for changes to take effect.
98+
If you add new permission scopes or Slack app features after an app has been installed, you must reinstall the app to your workspace for changes to take effect.
9599

96100
## Multiple Workspace Install
97101

@@ -109,9 +113,9 @@ token once it is granted. The client ID and client secret are available
109113
from your [app's configuration page](https://api.slack.com/apps). The
110114
scopes are determined by the functionality of the app — every method
111115
you wish to access has a corresponding scope and your app will need to
112-
request that scope in order to be able to access the method. Review the [full list of Slack OAuth scopes](https://api.slack.com/scopes).
116+
request that scope in order to be able to access the method. Review the [full list of Slack OAuth scopes](http://docs.slack.dev/reference/scopes).
113117

114-
``` python
118+
```python
115119
import os
116120
from slack_sdk import WebClient
117121
from flask import Flask, request
@@ -136,7 +140,7 @@ This link directs the user to the Slack OAuth acceptance page, where the
136140
user will review and accept or refuse the permissions your app is
137141
requesting as defined by the scope(s).
138142

139-
``` python
143+
```python
140144
@app.route("/slack/install", methods=["GET"])
141145
def pre_install():
142146
state = "randomly-generated-one-time-value"
@@ -154,7 +158,7 @@ will redirect the user to your auth completion page, which includes a
154158
[endpoint](https://api.slack.com/methods/oauth.v2.access) that will
155159
finally grant you the token.
156160

157-
``` python
161+
```python
158162
@app.route("/slack/oauth_redirect", methods=["GET"])
159163
def post_install():
160164
# Verify the "state" parameter
@@ -176,7 +180,7 @@ def post_install():
176180
A successful request to `oauth.v2.access` will yield a JSON payload with
177181
at least one token, a bot token that begins with `xoxb`.
178182

179-
``` python
183+
```python
180184
@app.route("/slack/oauth_redirect", methods=["GET"])
181185
def post_install():
182186
# Verify the "state" parameter
@@ -216,7 +220,7 @@ We recommend using [virtualenv
216220
(venv)](https://docs.python.org/3/tutorial/venv.html) to set up your
217221
Python runtime.
218222

219-
``` bash
223+
```bash
220224
# Create a dedicated virtual env for running your Python scripts
221225
python -m venv .venv
222226

@@ -233,7 +237,7 @@ export SLACK_BOT_TOKEN=xoxb-***
233237
Then, verify the following code works on the Python REPL (you can start
234238
it by just `python`).
235239

236-
``` python
240+
```python
237241
import os
238242
import logging
239243
from slack_sdk import WebClient
@@ -247,4 +251,4 @@ package. That being said, the code you're working on may be still using
247251
the old package. If you encounter an error saying
248252
`AttributeError: module 'slack' has no attribute 'WebClient'`, run
249253
`pip list`. If you find both `slack_sdk` and `slack` in the output, try
250-
removing `slack` by `pip uninstall slack` and reinstalling `slack_sdk`.
254+
removing `slack` by `pip uninstall slack` and reinstalling `slack_sdk`.

0 commit comments

Comments
 (0)