我使用 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/