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

Python slackclient oauth.access call returning 'invalid_code' error

我正在使用 ngrok + flask + python slackclient 来响应 Slack API 的 OAuth Flow,我收到一个 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 工作区的“管理应用程序”页面中的 "Add" 按钮启动应用程序安装。我可以确认我在安装启动后收到了预期的 code,并且它被正确地传递到最终将请求发送到 https://slack.com/api/oauth.accessslack.BaseClient.api_call() 函数。

我希望 oauth_access 调用的响应是包含我的访问令牌的 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 的问题。我认为这个 pull request 解决了这个问题

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

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

此问题已在 slackclient

v2.2.1 中解决

changelog:

[WebClient] Oauth previously failed to pass along credentials properly. This is fixed now. #527