How to integrate with Slack OAuth in Flask

shyamady profile image Shun Yamada ・2 min read

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.


If you knew more about Slack App, please check this out. I wrote about it before.

An official doc


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_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.

@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

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

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.

   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.

Posted on by:


markdown guide