无法使用API获取空闲用户配置文件信息

2024-04-19 18:31:40 发布

您现在位置:Python中文网/ 问答频道 /正文

事先非常感谢。我正在尝试通过slack_authentication获取用户配置文件信息。虽然应用程序已成功通过Slack身份验证,但它无法获取电子邮件用户名。你知道吗

{'ok': True, 'access_token': 'xoxp-xXXXXXXXXXXXXXXXX', 'scope': 'identify,channels:read,users.profile:read,chat:write:bot,identity.basic', 'user_id': 'XXXXXXXXX', 'team_id': 'XXXXXXXX', 'enterprise_id': None, 'team_name': 'test', 'warning': 'superfluous_charset', 'response_metadata': {'warnings': ['superfluous_charset']}}

我尝试添加identify范围而不是identity.basic,因为slack不允许同时使用identity.basic和其他范围。你知道吗

代码如下:

@bp.route('/redirect', methods=['GET'])
def authorize():
    authorize_url = f"https://slack.com/oauth/authorize?scope={ oauth_scope }&client_id={ client_id }"

    return authorize_url

@bp.route('/callback', methods=["GET", "POST"])
def callback():
    auth_code = request.args['code']
    client = slack.WebClient(token="")
    response = client.oauth_access(
        client_id=client_id,
        client_secret=client_secret,
        code=auth_code
    )
    print(response)

附加

我已经意识到如何获得users info。我把代码更新成这样。你知道吗

代码更新如下:

    oauth = client.oauth_access(
        client_id=client_id,
        client_secret=client_secret,
        code=auth_code
    )
    user_id = oauth['user_id']
    response = client.users_info(user=user_id)

但出现以下错误:

The server responded with: {'ok': False, 'error': 'not_authed'}


Tags: 代码clientidsecretaccessbasicresponsecode
1条回答
网友
1楼 · 发布于 2024-04-19 18:31:40

您的代码看起来像是使用OAuth的Slack应用程序的安装例程。但它不包含获取用户配置文件的调用。你知道吗

要获取用户的配置文件,可以调用^{},并提供感兴趣的用户的ID。你知道吗

示例:

response = client.users_info(user=ID_OF_USER)
assert(response)
profile = response['user']['profile']
email = response['user']['profile']['email']

为了检索用户的配置文件和电子邮件地址,您需要以下范围: - 用户:读取 - 用户:read.email你知道吗

标识作用域与用户配置文件无关。它们仅用于“Sign-in with Slack”方法,您可以在第三方网站上向空闲用户进行身份验证。你知道吗

最后,请澄清一下,因为这经常被误解:您只需要运行一次OAuth安装例程。该例程将为工作区生成一个令牌,您可以将其存储并用于对该工作区的API的任何进一步调用。你知道吗

更新为“附加”

您没有正确使用API。你知道吗

您需要首先完成Oauth流并收集访问令牌,它位于来自client.oauth_access的响应中。你知道吗

然后您需要用收到的令牌初始化一个新的WebClient。使用新客户机,您可以访问所有API方法,如用户信息等等

同样:您应该只运行一次OAuth进程,并存储接收到的令牌以供以后使用。你知道吗

示例:

oauth_info = client.oauth_access(
    client_id=client_id,
    client_secret=client_secret,
    code=auth_code
)
access_token = oauth_info['access_token'] # you want to store this token in a database

client = slack.WebClient(token=access_token)
user_id = oauth_info['user_id']
response = client.users_info(user=user_id)
print(response)

相关问题 更多 >