使用 requests python 访问不和谐 API 会导致 401:未经授权

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

我想制作一个脚本,在授权时,将我的替代品拉到不和谐的服务器上。

使用请求+烧瓶我尝试发出一个以 401 Unauthorized 结尾的 PUT 请求

# Oauth.discord_api_url = "https://discord.com/api"
# at = Access token (it works)
headers = {"Authorization": f"Bearer {at}"}
guildid = *insert id here*
urlguildy = f"{Oauth.discord_api_url}/guilds/{guildid}/members/{userid}"
r = requests.put(url=urlguildy, headers=headers).json() # 401 Unauthorized

我有另一个显示公会列表的命令,它工作得很好,但这只是返回 401。如果有人提供帮助,我会很高兴。

python python-requests discord
1个回答
0
投票

我通过使用 BOT TOKEN 作为标头和 ACCESS TOKEN 作为 json 参数来使其工作。因此它会获取服务器上的应用程序机器人并执行一些授权操作。一段代码:

        endpoint = f"{Oauth.discord_api_url}/guilds/{guildid}/members/{userid}"
        bot_token = "yes"
        botheader = {"Authorization": f"Bot {bot_token}", "Content-Type": "application/json"}
        payload = {"access_token": access_token}
        r = requests.put(url=endpoint, headers=botheader, json=payload).json()

(它在我要使用的函数中)

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