Search code examples
pythonslack

Can't fetch slack user profile information with API


Thank you so much in advance. I am trying to fetch user profile information through slack_authentication. Although the app is successfully authenticated with Slack, it could not get email and username.

{'ok': True, 'access_token': 'xoxp-xXXXXXXXXXXXXXXXX', 'scope': 'identify,channels:read,users.profile:read,chat:write:bot,identity.basic', 'user_id': 'XXXXXXXXX', 'team_id': 'XXXXXXXX', 'enterprise_id': None, 'team_name': 'test', 'warning': 'superfluous_charset', 'response_metadata': {'warnings': ['superfluous_charset']}}

I tried to add identify scope instead of identity.basic because slack doesn't allow you to use both identity.basic and other scopes.

The code is below:

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

    return authorize_url

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

Additional

I have realized how to get users info. I updated the code to like this.

The code is updated like below:

    oauth = client.oauth_access(
        client_id=client_id,
        client_secret=client_secret,
        code=auth_code
    )
    user_id = oauth['user_id']
    response = client.users_info(user=user_id)

But this error occurs:

The server responded with: {'ok': False, 'error': 'not_authed'}


Solution

  • Your code looks like an installation routine for a Slack app using OAuth. But it does not contain a call to get a user profile.

    To get the profile of a user you can call users.info and provide the ID of the user you are interested in.

    Examples:

    response = client.users_info(user=ID_OF_USER)
    assert(response)
    profile = response['user']['profile']
    email = response['user']['profile']['email']
    

    In order to retrieve the user's profile and email address you need these scopes: - users:read - users:read.email

    The identity scopes are unrelated to the user profile. They are used for the "Sign-in with Slack" approach only, where you can authenticate with a Slack user on a 3rd party web site.

    Finally, just to clarify, because this is often misunderstood: You only need to run through the OAuth installation routine once. The routine will yield you a token for the workspace, which you can store and use for any further calls to the API for that workspace.

    Update to "Additional"

    You are not using the API correctly.

    You need to first complete the Oauth flow and collect the access token, which is in the response from client.oauth_access.

    Then you need to initialize a new WebClient with the token you received. With the new client you can then access all API methods, like users.info etc.

    Again: You should run through the OAuth process one time only and store the received token for later use.

    Example:

    oauth_info = client.oauth_access(
        client_id=client_id,
        client_secret=client_secret,
        code=auth_code
    )
    access_token = oauth_info['access_token'] # you want to store this token in a database
    
    client = slack.WebClient(token=access_token)
    user_id = oauth_info['user_id']
    response = client.users_info(user=user_id)
    print(response)