Python slackclient oauth.access调用返回“无效代码”错误

问题描述 投票:1回答:1

我正在使用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工作区的“管理应用程序”页面中的“添加”按钮启动应用程序安装。启动安装后,我可以确认收到了预期的code,并且已正确传递给slack.BaseClient.api_call()函数,该函数最终将请求发送到https://slack.com/api/oauth.access

我希望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 flask slack slack-api
1个回答
0
投票

这似乎是Python SDK的问题。我认为此拉取请求可以解决此问题

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

© www.soinside.com 2019 - 2024. All rights reserved.