Python slackclient oauth.access 调用返回 'invalid_code' 错误

标签 python flask slack slack-api

我使用 ngrok + Flask + python slackclient 响应 Slack API 的 OAuth 流程,但收到 invalid_code 错误。

我正在执行 slackclient docs 中描述的步骤到目前为止我的代码非常简单:

import os
import slack
from flask import Flask, request
app = Flask(__name__)

SLACK_CLIENT_ID = os.environ['SLACK_CLIENT_ID']
SLACK_CLIENT_SECRET = os.environ['SLACK_CLIENT_SECRET']

@app.route('/install', methods=['GET', 'POST'])
def install():
    # Retrieve the auth code from the request params
    auth_code = request.args['code']

    # An empty string is a valid token for this request
    client = slack.WebClient(token='')

    # Request the auth tokens from Slack
    response = client.oauth_access(
        client_id=SLACK_CLIENT_ID,
        client_secret=SLACK_CLIENT_SECRET,
        code=auth_code
    )

    print(response)

if __name__ == '__main__':
    app.run()

我通过 Slack 工作区的“管理应用程序”页面中的“添加”按钮启动应用程序安装。我可以确认安装启动后我收到了预期的 code,并且它被正确传递到了 slack.BaseClient.api_call() 函数,最终将请求发送到 https://slack.com/api/oauth.access

我希望 oauth_access 调用的响应是一个包含我的访问 token 的 JSON 对象,但是,我得到:

slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'invalid_code', 'warning': 'superfluous_charset', 'response_metadata': {'warnings': ['superfluous_charset']}}

我尝试将带有所需参数的带有curl的POST发送到Slack的端点,并且它按预期工作。我还尝试了 requests.post() 并且也按预期工作。所以我怀疑我错误地使用了 slackclient 或者误解了某些东西。谁能帮我指出正确的方向?

最佳答案

这似乎是Python SDK的问题。我认为这个拉取请求解决了这个问题

https://github.com/slackapi/python-slackclient/pull/527

与此同时,恢复到 2.1.0 版本可能会更容易

关于Python slackclient oauth.access 调用返回 'invalid_code' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231065/

相关文章:

python - Flask 和 SqlAlchemy session

python - 如何使用 Python 获取自定义松弛斜杠命令列表?

slack-api - 如何在 Slack 中检测新用户?

python - 使用 cPickle 仅返回文件中的第一个条目

python - 应用程序启动错误: CreateProcess: The system cannot find the file specified

python - Django strip_tags 模板过滤添加空格

python - 将数据集与绘图进行比较 - 将虚线与实线混合

python - (flask)-sqlalchemy查询,必须导入所有模型

python - 光滑的旋转木马在 Flask 中不起作用

python - 在 python 中处理 Slack 按钮操作 POST 请求数据