How to integrate with Slack OAuth in Flask
Tags: flask, python, slack
Published: 2019-12-03 10:32:21

---
title: How to integrate with Slack OAuth in Flask
published: true
description: I tried to integrate with Slack OAuth in Flask. Actually, you can make it easy to understand once you read an official document I assume. But some errors occurred to me then, for example, to get user details. So here it is a tutorial to authenticate successfully.
tags: Flask, Python, Slack
---

I tried to integrate with Slack OAuth in Flask. Actually, you can make it easy to understand once you read an official document I assume. But some errors occurred to me then, for example, to get user details. So here it is a tutorial to authenticate successfully.

## Reference
If you knew more about Slack App, please check this out. I wrote about it before.
[https://dev.to/shyamady/how-to-publish-slack-app-using-rails-5hlm](https://dev.to/shyamady/how-to-publish-slack-app-using-rails-5hlm)

An official doc
[https://slack.dev/python-slackclient/auth.html](https://slack.dev/python-slackclient/auth.html)

## Code

```python
import functools
import pdb
import slack

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)

from app.db import (
    search_user, register_user, search_team, register_team, search_team_user, register_team_user
)

bp = Blueprint('auth', __name__, url_prefix='/auth')
client_id = 'XXXXXXXXXXXXXXX'
client_secret = 'XXXXXXXXXXXXXXX'
oauth_scope = 'channels:read,chat:write:bot,users:read,users:read.email'
```

Define `client_id` and `client_secret` initially. In addition, you need to put `scope` here. `scope` should be included what you request for users. Not only for the app, but you also need to register ones on Slack App. You suppose to write why you need these scopes when submitting.

In this case, I added `channels:read` and `chat:write:bot` to this. Also, it's necessary to  set `users:read` and `users:read.email` for fetching users detail.


```python
@bp.route('/redirect', methods=['GET'])
def authorize():
    authorize_url = f"https://slack.com/oauth/authorize?scope={ oauth_scope }&client_id={ client_id }"

    return redirect(authorize_url)
```

See `/auth/redirect`

```python
@bp.route('/callback', methods=["GET", "POST"])
def callback():
    auth_code = request.args['code']
    client = slack.WebClient(token="")
    oauth_info = client.oauth_access(
        client_id=client_id,
        client_secret=client_secret,
        code=auth_code
    )
```

After the callback, then move right along. You can get basic information with `oauth_access`.

However, this response is not supposed to include personal information like username, email, and etc.


```python
   access_token = oauth_info['access_token']
   client = slack.WebClient(token=access_token)
   user_id = oauth_info['user_id']
   response = client.users_info(user=user_id)
```

`users_info` with `access_token` leads you to get almost all user's information like username, email, icon, and timezone.

I hope you can use this code in Django or other Python frameworks. As I am a Rails engineer, there're not too many references concerns for Flask.

But I believe it will be increasing its number with Machine Learning potentiality.