|
| 1 | +--- |
| 2 | +title: Authenticating with OAuth |
| 3 | +lang: en |
| 4 | +slug: authenticating-oauth |
| 5 | +order: 14 |
| 6 | +--- |
| 7 | + |
| 8 | +<div class="section-content"> |
| 9 | + |
| 10 | +Slack apps installed on multiple workspaces will need to implement OAuth, then store installation information (like access tokens) securely. By providing `client_id`, `client_secret`, `scopes`, `installation_store`, and `state_store` when initializing App, Bolt for Python will handle the work of setting up OAuth routes and verifying state. If you’re implementing a custom receiver, you can make use of our [OAuth library](https://slack.dev/python-slack-sdk/oauth), which is what Bolt for Python uses under the hood. |
| 11 | + |
| 12 | +Bolt for Python will create a **Redirect URL** `slack/oauth_redirect`, which Slack uses to redirect users after they complete your app’s installation flow. You will need to add this **Redirect URL** in your app configuration settings under **OAuth and Permissions**. This path can be configured in the `OAuthSettings` argument described below. |
| 13 | + |
| 14 | +Bolt for Python will also create a `slack/install` route, where you can find an **Add to Slack** button for your app to perform direct installs of your app. If you need any additional authorizations (user tokens) from users inside a team when your app is already installed or a reason to dynamically generate an install URL, you can pass your own custom URL generator to `oauth_settings` as `authorize_url_generator`. |
| 15 | + |
| 16 | +To learn more about the OAuth installation flow with Slack, [read the API documentation](https://api.slack.com/authentication/oauth-v2). |
| 17 | + |
| 18 | +</div> |
| 19 | + |
| 20 | +```python |
| 21 | +oauth_settings = OAuthSettings( |
| 22 | + client_id=os.environ["SLACK_CLIENT_ID"], |
| 23 | + client_secret=os.environ["SLACK_CLIENT_SECRET"], |
| 24 | + scopes=["channels:read", "groups:read", "chat:write"], |
| 25 | + installation_store=FileInstallationStore(), |
| 26 | + state_store=FileOAuthStateStore(expiration_seconds=120) |
| 27 | +) |
| 28 | + |
| 29 | +app = App(signing_secret=os.environ["SIGNING_SECRET"], |
| 30 | + oauth_settings=oauth_settings) |
| 31 | +``` |
| 32 | + |
| 33 | +<details class="secondary-wrapper"> |
| 34 | +<summary class="section-head" markdown="0"> |
| 35 | +<h4 class="section-head">Customizing OAuth defaults</h4> |
| 36 | +</summary> |
| 37 | + |
| 38 | +<div class="secondary-content" markdown="0"> |
| 39 | +You can override the default OAuth using `oauth_settings`, which can be passed in during the initialization of App. You can override the following: |
| 40 | + |
| 41 | +- `authorization_url`: Used to toggle between new Slack Apps and Classic Slack Apps |
| 42 | +- `install_path`: Override default path for "Add to Slack" button |
| 43 | +- `redirect_uri`: Override default redirect url path |
| 44 | +- `callback_options`: Provide custom success and failure pages at the end of the OAuth flow |
| 45 | +- `state_store`: Provide a custom state store instead of using the built in `OAuthStateStore` |
| 46 | + |
| 47 | +</div> |
| 48 | + |
| 49 | +```python |
| 50 | +oauth_settings = OAuthSettings( |
| 51 | + client_id=os.environ["SLACK_CLIENT_ID"], |
| 52 | + client_secret=os.environ["SLACK_CLIENT_SECRET"], |
| 53 | + scopes=["channels:read", "groups:read", "chat:write", "incoming-webhook"], |
| 54 | + installation_store=FileInstallationStore(), |
| 55 | + state_store=FileOAuthStateStore(expiration_seconds=120), |
| 56 | + install_path="/slack/install_app", |
| 57 | + redirect_uri_path="/slack/redirect", |
| 58 | + callback_options=CallbackOptions(success=success_handler, |
| 59 | + failure=failure_handler) |
| 60 | +) |
| 61 | + |
| 62 | +app = App(signing_secret=os.environ["SIGNING_SECRET"], |
| 63 | + oauth_settings=oauth_settings) |
| 64 | +``` |
| 65 | + |
| 66 | +</details> |
0 commit comments