我已将 slack-bolt Django 示例合并到我的 Django 项目中。 我可以使用 /slack/install 成功安装该应用程序。 然而,我在定制方面遇到了挑战。
我的应用程序已按照 django 示例进行初始化
app = App(
signing_secret=signing_secret,
oauth_settings=OAuthSettings(
client_id=client_id,
client_secret=client_secret,
scopes=scopes,
user_scopes=user_scopes,
# If you want to test token rotation, enabling the following line will make it easy
# token_rotation_expiration_minutes=1000000,
installation_store=DjangoInstallationStore(
client_id=client_id,
logger=logger,
),
state_store=DjangoOAuthStateStore(
expiration_seconds=120,
logger=logger,
),
),
)
我希望从我的应用程序的 /profile 页面触发安装流程。 我生成了 Slack 安装按钮并将其放置在我的应用程序/个人资料页面中。请注意,用户在通过 django 项目身份验证后即可使用个人资料页面。
当用户被重定向时,Slack 页面会显示,用户单击“允许”并被重定向回 /slack/oauth_redirect 显示错误并显示 Slack 安装是从与 /slack/install 不同的 URL 触发的信息。
我尝试在我的应用程序中设置installation_url,如下所示 app.oauth_flow.install_path = '/profile' app.oauth_flow.settings.install_path = '/profile'
但是没有成功
我可以让它工作的唯一方法是禁用状态验证 app.oauth_flow.settings.state_validation_enabled = False
我将欣赏一个展示如何执行此操作的代码示例。
当用户批准 Slack 应用程序范围时,用户将被重定向回 /oauth_redirect 以完成应用程序安装(将数据保存到数据库)。我希望在保存所有设置以及成功和失败安装的附加查询字符串参数后,将用户重定向回 /profile 页面。
我尝试设置以下内容,但不起作用
app.oauth_flow.settings.redirect_uri =“/配置文件”
app.oauth_flow.settings.success_url =“/profile?slack_install=1”
app.oauth_flow.settings.failure_url =“/profile?slack_install=0”
可重现: slack_bolt 版本 松紧螺栓==1.18.1
Python运行时版本 Python 3.9.5
操作系统信息 产品名称: macOS 产品版本:14.2.1 构建版本:23C71 Darwin 内核版本 23.2.0:2023 年 11 月 15 日星期三 21:53:34(太平洋标准时间);根目录:xnu-10002.61.3~2/RELEASE_ARM64_T8103
我尝试更改 oauth_flow 设置,但没有成功。
对于挑战 1,您可以在 OAuthSettings 中使用
install_page_rendering_enabled=False
,这将允许您从任何 URL 安装,但不要忘记配置 /slack/install
端点并将其作为 install_path="/slack/install/",
传递给 OAuthSettings
对于挑战 2,请查看 slack 中的 official oauth django 示例。
对于挑战 3,请使用 OAuthSettings 上的回调选项。这是我的有效片段。
def success(args: SuccessArgs) -> BoltResponse:
# Use the args to update stuff in your system
return BoltResponse(
status=200,
body="Yay! Thanks for installing our slackbot.",
)
def failure(args: FailureArgs) -> BoltResponse:
return BoltResponse(status=args.suggested_status_code, body=args.reason)
slack_app = App(
signing_secret=settings.SLACK_SIGNING_SECRET,
oauth_settings=OAuthSettings(
client_id=settings.SLACK_CLIENT_ID,
client_secret=settings.SLACK_CLIENT_SECRET,
scopes=[
"app_mentions:read",
"channels:history",
"channels:read",
"chat:write",
"users:read",
],
# If you want to test token rotation, enabling the following line will make it easy
# token_rotation_expiration_minutes=1000000,
installation_store=DjangoInstallationStore(
client_id=settings.SLACK_CLIENT_ID,
),
state_store=DjangoOAuthStateStore(
expiration_seconds=120,
),
install_page_rendering_enabled=False,
install_path="/slack/install/",
redirect_uri_path="/slack/oauth-redirect/",
callback_options=CallbackOptions(success=success, failure=failure),
),
)