forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.html
More file actions
383 lines (357 loc) · 28.7 KB
/
auth.html
File metadata and controls
383 lines (357 loc) · 28.7 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<
8000
/div>
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type"
content="text/html; charset=utf-8"
/>
<meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Tokens & Installation — slackclient (Legacy Python Slack SDK)</title>
<!-- Google Tag Manager -->
<script>
(function(w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-KFZ5MK7');
</script>
<!-- End Google Tag Manager -->
<link
href="https://a.slack-edge.com/4f227/style/rollup-slack_kit_legacy_adapters.css"
rel="stylesheet"
type="text/css"
/>
<link
href="https://a.slack-edge.com/3e02c0/style/rollup-api_site.css"
rel="stylesheet"
type="text/css"
/>
<link
rel="stylesheet"
href="./assets/default.css"
type="text/css"
/>
<link
rel="stylesheet"
href="./assets/pygments.css"
type="text/css"
/>
<link
rel="stylesheet"
href="./assets/docs.css"
type="text/css"
/>
<link
id="favicon"
rel="shortcut icon"
href="https://a.slack-edge.com/4f28/img/icons/favicon-32.png"
type="image/png"
/>
<link
rel="top"
title="slackclient (Legacy Python Slack SDK) 1.0.1 documentation"
href="index.html"
/>
</head>
<body class="api light_theme">
<!-- Google Tag Manager (noscript) -->
<noscript
><iframe
src="https://www.googletagmanager.com/ns.html?id=GTM-KFZ5MK7"
height="0"
width="0"
style="display:none;visibility:hidden"
></iframe
></noscript>
<!-- End Google Tag Manager (noscript) -->
<header>
<a id="menu_toggle" class="no_transition show_on_mobile">
<span class="menu_icon"></span>
<span class="vert_divider"></span>
</a>
<a
href="https://api.slack.com/"
id="header_logo"
class="api hide_on_mobile"
style="float:left; display: inline-block;"
>
<img
alt="Slack API"
src="https://a.slack-edge.com/3026cb/img/slack_api_logo_vogue.png"
style="width: 225px; padding-right: 25px; border-right: 1px solid #DDD;"
/>
</a>
<span
style="display: inline-block; padding-left: 20px; margin-top: 25px; font-weight: bold;"
>
<a style="color: #555459;" href="./index.html">slackclient (Legacy Python Slack SDK)</a>
</span>
<div class="header_nav">
<a
href="https://slack.dev/python-slack-sdk/"
class="btn header_btn"
data-qa="go_to_slack"
>The Latest SDK</a>
<a
href="https://github.com/SlackAPI/python-slackclient"
class="btn header_btn float_right"
data-qa="go_to_slack"
>Go to GitHub</a>
</div>
</header>
<div id="page">
<div id="page_contents" class="clearfix">
<!-- Sidebar Content -->
<nav id="api_nav" class="col span_1_of_4">
<div id="api_sections">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="index.html">Important Notice</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Tokens & Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#keeping-tokens-safe">Keeping tokens safe</a></li>
<li class="toctree-l2"><a class="reference internal" href="#single-workspace-install">Single Workspace Install</a></li>
<li class="toctree-l2"><a class="reference internal" href="#multiple-workspace-install">Multiple Workspace Install</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="basic_usage.html">Basic Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#sending-a-message">Sending a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#formatting-with-block-kit">Formatting with Block Kit</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#threading-messages">Threading Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#updating-a-message">Updating a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#deleting-a-message">Deleting a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#opening-a-modal">Opening a modal</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#updating-and-pushing-modals">Updating and pushing modals</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#emoji-reactions">Emoji reactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#listing-public-channels">Listing public channels</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#getting-a-channel-s-info">Getting a channel’s info</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#joining-a-channel">Joining a channel</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#leaving-a-channel">Leaving a channel</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#listing-team-members">Listing team members</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#uploading-files">Uploading files</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#calling-any-api-methods">Calling any API methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_usage.html#web-api-rate-limits">Web API Rate Limits</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="conversations.html">Conversations API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#direct-messages">Direct messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#creating-channels">Creating channels</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#getting-more-information">Getting more information</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#listing-conversations">Listing conversations</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#leaving-a-conversation">Leaving a conversation</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#getting-members">Getting members</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="real_time_messaging.html">Real Time Messaging (RTM)</a><ul>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#configuring-the-rtm-api">Configuring the RTM API</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#connecting-to-the-rtm-api">Connecting to the RTM API</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#rtm-start-vs-rtm-connect">rtm.start vs rtm.connect</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#rtm-events">RTM Events</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="faq.html#i-cannot-install-slackclient">I cannot install slackclient…</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#should-i-go-with-run-async">Should I go with run_async?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#i-found-a-bug">I found a bug!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#there-s-a-feature-missing">There’s a feature missing!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#how-do-i-contribute">How do I contribute?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#how-do-i-compile-the-documentation">How do I compile the documentation?</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a><ul>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v3-0-0-2020-11-09">v3.0.0 (2020-11-09)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-9-3-2020-10-20">v2.9.3 (2020-10-20)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-9-2-2020-10-09">v2.9.2 (2020-10-09)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-9-1-2020-09-23">v2.9.1 (2020-09-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-9-0-2020-09-17">v2.9.0 (2020-09-17)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-8-2-2020-09-04">v2.8.2 (2020-09-04)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-8-1-2020-08-28">v2.8.1 (2020-08-28)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-8-0-2020-08-06">v2.8.0 (2020-08-06)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-7-3-2020-07-20">v2.7.3 (2020-07-20)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-7-2-2020-06-23">v2.7.2 (2020-06-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-7-1-2020-06-04">v2.7.1 (2020-06-04)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-7-0-2020-06-02">v2.7.0 (2020-06-02)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-6-2-2020-05-28">v2.6.2 (2020-05-28)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-6-1-2020-05-24">v2.6.1 (2020-05-24)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-6-0-2020-05-21">v2.6.0 (2020-05-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-5-0-2019-12-09">v2.5.0 (2019-12-09)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-4-0-2019-11-27">v2.4.0 (2019-11-27)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-3-1-2019-10-29">v2.3.1 (2019-10-29)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-3-0-2019-10-22">v2.3.0 (2019-10-22)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-2-1-2019-10-08">v2.2.1 (2019-10-08)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-2-0-2019-09-25">v2.2.0 (2019-09-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-1-0-2019-07-01">v2.1.0 (2019-07-01)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-0-0-2019-04-29">v2.0.0 (2019-04-29)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-3-1-2019-02-28">v1.3.1 (2019-02-28)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-3-0-2018-09-11">v1.3.0 (2018-09-11)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-2-1-2018-03-26">v1.2.1 (2018-03-26)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-2-0-2018-03-20">v1.2.0 (2018-03-20)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-3-2018-03-01">v1.1.3 (2018-03-01)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-2-2018-01-31">v1.1.2 (2018-01-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-1-2018-01-30">v1.1.1 (2018-01-30)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-0-2017-11-21">v1.1.0 (2017-11-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-9-2017-08-31">v1.0.9 (2017-08-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-8-2017-08-31">v1.0.8 (2017-08-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-7-2017-08-02">v1.0.7 (2017-08-02)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-6-2017-06-12">v1.0.6 (2017-06-12)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-5-2017-01-23">v1.0.5 (2017-01-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-4-2016-12-15">v1.0.4 (2016-12-15)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-3-2016-12-13">v1.0.3 (2016-12-13)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-2-2016-09-22">v1.0.2 (2016-09-22)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-1-2016-03-25">v1.0.1 (2016-03-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-0-2016-02-28">v1.0.0 (2016-02-28)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v0-18-0-2016-02-21">v0.18.0 (2016-02-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v0-17-0-2016-02-15">v0.17.0 (2016-02-15)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a><ul>
<li class="toctree-l2"><a class="reference internal" href="about.html#product-name">slackclient (Legacy Python Slack SDK)</a></li>
</ul>
</li>
</ul>
<div id="footer">
<ul id="footer_nav">
<li><a href="https://github.com/SlackAPI/python-slackclient/blob/main/LICENSE">License</a></li>
<li><a href="https://slackhq.github.io/code-of-conduct">Code of Conduct</a></li>
<li><a href="https://github.com/slackapi/python-slackclient/blob/main/.github/contributing.md">Contributing</a></li>
<li><a href="https://docs.google.com/a/slack-corp.com/forms/d/e/1FAIpQLSfzjVoCM7ohBnjWf7eDYQxzti1EPpinsIJQA5RAUBwJKRUQHg/viewform">Contributor License Agreement</a></li>
</ul>
<p id="footer_signature">Made with <i class="ts_icon ts_icon_heart"></i> by Slack<br/>and our Lovely
Community
</p>
</div>
</div>
</nav>
<!-- /Sidebar Content -->
<!-- Body Content -->
<div class="col span_3_of_4">
<!-- <div class="section-title">Tokens & Installation</div> -->
<div class="card">
<section id="tokens-installation">
<h1>Tokens & Installation<a class="headerlink" href="#tokens-installation" title="Permalink to this headline">¶</a></h1>
<section id="keeping-tokens-safe">
<span id="handling-tokens"></span><h2>Keeping tokens safe<a class="headerlink" href="#keeping-tokens-safe" title="Permalink to this headline">¶</a></h2>
<p>The OAuth token you use to call the Slack API has access to the data on the workspace where it is installed. Depending on the scopes granted to the token, it potentially has the ability to read and write data. Treat these tokens just as you would a password – don’t publish them, don’t check them into source code, don’t share them with others.</p>
<p>🚫Avoid this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">token</span> <span class="o">=</span> <span class="s1">'xoxb-111-222-xxxxx'</span>
</pre></div>
</div>
<p>We recommend you pass tokens in as environment variables, or persist them in a database that is accessed at runtime. You can add a token to the environment by starting your app as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">SLACK_BOT_TOKEN</span><span class="o">=</span><span class="s2">"xoxb-111-222-xxxxx"</span> <span class="n">python</span> <span class="n">myapp</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>Then retrieve the key with:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">SLACK_BOT_TOKEN</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"SLACK_BOT_TOKEN"</span><span class="p">]</span>
</pre></div>
</div>
<p>For additional information, please see our <a class="reference external" href="https://api.slack.com/authentication/best-practices">Safely Storing Credentials</a> page.</p>
</section>
<section id="single-workspace-install">
<h2>Single Workspace Install<a class="headerlink" href="#single-workspace-install" title="Permalink to this headline">¶</a></h2>
<p>If you’re building an application for a single Slack workspace, there’s no need to build out the entire OAuth flow.</p>
<p>Once you’ve setup your features, click on the <strong>Install App to Team</strong> button found on the <strong>Install App</strong> page.
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.</p>
<p>For additional information, see the <a class="reference external" href="https://api.slack.com/start/overview#installing_distributing">Installing Apps</a> of our <a class="reference external" href="https://api.slack.com/start">Building Slack apps</a> page.</p>
</section>
<section id="multiple-workspace-install">
<h2>Multiple Workspace Install<a class="headerlink" href="#multiple-workspace-install" title="Permalink to this headline">¶</a></h2>
<p>If you intend for an app to be installed on multiple Slack workspaces, you will need to handle this installation via the industry-standard OAuth protocol. You can read more about <a class="reference external" href="https://api.slack.com/authentication/oauth-v2">how Slack handles Oauth</a>.</p>
<p>(The OAuth exchange is facilitated via HTTP and requires a webserver; in this example, we’ll use <a class="reference external" href="https://flask.palletsprojects.com/">Flask</a>.)</p>
<p>To configure your app for OAuth, you’ll need a client ID, a client secret, and a set of one or more scopes that will be applied to the token once it is granted. The client ID and client secret are available from your <a class="reference external" href="https://api.slack.com/apps">app’s configuration page</a>. The scopes are determined by the functionality of the app – every method you wish to access has a corresponding scope and your app will need to request that scope in order to be able to access the method. Review Slack’s <a class="reference external" href="https://api.slack.com/scopes">full list of OAuth scopes</a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">slack</span> <span class="kn">import</span> <span class="n">WebClient</span>
<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">request</span>
<span class="n">client_id</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"SLACK_CLIENT_ID"</span><span class="p">]</span>
<span class="n">client_secret</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"SLACK_CLIENT_SECRET"</span><span class="p">]</span>
<span class="n">oauth_scope</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"SLACK_SCOPES"</span><span class="p">]</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>The OAuth initiation link</strong></p>
<p>To begin the OAuth flow that will install your app on a workspace, you’ll need to provide the user with a link to Slack’s OAuth page. This can be a simple link to <code class="docutils literal notranslate
8000
"><span class="pre">https://slack.com/oauth/v2/authorize</span></code> with <code class="docutils literal notranslate"><span class="pre">scope</span></code> and <code class="docutils literal notranslate"><span class="pre">client_id</span></code> query parameters, or you can use our pre-built <a class="reference external" href="https://api.slack.com/docs/slack-button">Add to Slack button</a> to do all the work for you.</p>
<p>This link directs the user to Slack’s OAuth acceptance page, where the user will review and accept or refuse the permissions your app is requesting as defined by the scope(s).</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">"/slack/install"</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s2">"GET"</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">pre_install</span><span class="p">():</span>
<span class="n">state</span> <span class="o">=</span> <span class="s2">"randomly-generated-one-time-value"</span>
<span class="k">return</span> <span class="s1">'<a href="https://slack.com/oauth/v2/authorize?'</span> \
<span class="sa">f</span><span class="s1">'scope=</span><span class="si">{</span><span class="n">oauth_scope</span><span class="si">}</span><span class="s1">&client_id=</span><span class="si">{</span><span class="n">client_id</span><span class="si">}</span><span class="s1">&state=</span><span class="si">{</span><span class="n">state</span><span class="si">}</span><span class="s1">">'</span> \
<span class="s1">'Add to Slack</a>'</span>
</pre></div>
</div>
<p><strong>The OAuth completion page</strong></p>
<p>Once the user has agreed to the permissions you’ve requested, Slack will redirect the user to your auth completion page, which includes a <code class="docutils literal notranslate"><span class="pre">code</span></code> query string param. You’ll use the <code class="docutils literal notranslate"><span class="pre">code</span></code> param to call the <code class="docutils literal notranslate"><span class="pre">oauth.v2.access</span></code> <a class="reference external" href="https://api.slack.com/methods/oauth.v2.access">endpoint</a> that will finally grant you the token.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">"/slack/oauth_redirect"</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s2">"GET"</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">post_install</span><span class="p">():</span>
<span class="c1"># Verify the "state" parameter</span>
<span class="c1"># Retrieve the auth code from the request params</span>
<span class="n">code_param</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'code'</span><span class="p">]</span>
<span class="c1"># An empty string is a valid token for this request</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">WebClient</span><span class="p">()</span>
<span class="c1"># Request the auth tokens from Slack</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">oauth_v2_access</span><span class="p">(</span>
<span class="n">client_id</span><span class="o">=</span><span class="n">client_id</span><span class="p">,</span>
<span class="n">client_secret</span><span class="o">=</span><span class="n">client_secret</span><span class="p">,</span>
<span class="n">code</span><span class="o">=</span><span class="n">code_param</span>
<span class="p">)</span>
</pre></div>
</div>
<p>A successful request to <code class="docutils literal notranslate"><span class="pre">oauth.v2.access</span></code> will yield a JSON payload with at least one token, a bot token that begins with <code class="docutils literal notranslate"><span class="pre">xoxb</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">"/slack/oauth_redirect"</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s2">"GET"</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">post_install</span><span class="p">():</span>
<span class="c1"># Verify the "state" parameter</span>
<span class="c1"># Retrieve the auth code from the request params</span>
<span class="n">code_param</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'code'</span><span class="p">]</span>
<span class="c1"># An empty string is a valid token for this request</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">WebClient</span><span class="p">()</span>
<span class="c1"># Request the auth tokens from Slack</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">oauth_v2_access</span><span class="p">(</span>
<span class="n">client_id</span><span class="o">=</span><span class="n">client_id</span><span class="p">,</span>
<span class="n">client_secret</span><span class="o">=</span><span class="n">client_secret</span><span class="p">,</span>
<span class="n">code</span><span class="o">=</span><span class="n">code_param</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
<span class="c1"># Save the bot token to an environmental variable or to your data store</span>
<span class="c1"># for later use</span>
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"SLACK_BOT_TOKEN"</span><span class="p">]</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s1">'access_token'</span><span class="p">]</span>
<span class="c1"># Don't forget to let the user know that OAuth has succeeded!</span>
<span class="k">return</span> <span class="s2">"Installation is completed!"</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">3000</span><span class="p">)</span>
</pre></div>
</div>
<p>Once your user has completed the OAuth flow, you’ll be able to use the provided tokens to call any of Slack’s API methods that require an access token.</p>
<p>See the <a class="reference external" href="./basic_usage.html">Basic Usage</a> section of this documentation for usage examples.</p>
</section>
</section>
<div class="clear_both large_bottom_margin"></div>
</div>
</div>
<!-- /Body Content -->
</div>
</div>
<footer>
<p class="light tiny align_center">
© 2015- Slack Technologies, LLC and contributors
</p>
</footer>
<script>
window.ga =
window.ga ||
function() {
(ga.q = ga.q || []).push(arguments);
};
ga.l = +new Date();
ga('create', 'UA-56978219-13', 'auto');
ga('send', 'pageview');
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
</body>
</html>
You can’t perform that action at this time.