[Python] Slack sdk OAuth 2 redirect url not working

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I created a Slack app and I'm trying to get OAuth 2.0 to work. I'm using Python with flask to try and exchange tokens but I can't seem to get the redirect url to open properly.

    Slack requires the redirect to be https, and I'm using localhost to redirect at http://127.0.0.1:5000/slack/oauth/callback. It seems like Slack won't accept an http url, and flask doesn't work with https. Is there something I'm missing to get this working?

    I saw some other solutions to try and apply a proxy fix, but that didn't work for me.

    Thanks in advance and let me know if you need any other details.

    Here's what I'm currently doing right now:

    import html
    from slack_sdk.oauth import AuthorizeUrlGenerator
    from slack_sdk.oauth.installation_store import FileInstallationStore
    from slack_sdk.oauth.state_store import FileOAuthStateStore
    from flask import Flask


    SLACK_CLIENT_ID = "MY_CLIENT_ID"


    state_store = FileOAuthStateStore(expiration_seconds=300, base_dir="./data")
    installation_store = FileInstallationStore(base_dir="./data")

    # Build https://slack.com/oauth/v2/authorize with sufficient query parameters
    authorize_url_generator = AuthorizeUrlGenerator(
    client_id=SLACK_CLIENT_ID,
    scopes=["app_mentions:read", "chat:write"],
    user_scopes=["bookmarks:read", "bookmarks:write", "canvases:read", "canvases:write", "chat:write", "groups:history",
    "groups:read", "groups:write", "groups:write.invites", "groups:write.topic", "pins:read", "pins:write"],
    )


    app = Flask(__name__)


    @app.route("/slack/install", methods=["GET"])
    def oauth_start():
    state = state_store.issue()
    url = authorize_url_generator.generate(state)
    return f'<a href="{html.escape(url)}">' \
    f'<img alt=""Add to Slack"" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" /></a>'


    from slack_sdk.web import WebClient
    @app.route("/slack/oauth/callback", methods=["GET"])
    def oauth_callback():
    print("Hello world!") # Doesn't reach this


    # Redirect is https://127.0.0.1:5000/slack/oauth/callback
    if __name__ == "__main__":
    print("install at 'http://127.0.0.1:5000/slack/install'") # Opens ok
    app.run()

    Continue reading...

Compartilhe esta Página