嗨,我正在尝试执行“使用Discord授权”,以自动将用户加入我的行会。
我正在运行一个处理所有这些问题的Flask
应用程序。
到目前为止,这是我的代码:
def add_to_guild(access_token, userID, guildID):
url = f"{Oauth.discord_api_url}/guilds/{guildID}/members/{userID}"
headers = {
"Authorization" : f"Bearer {access_token}"
}
response = requests.post(url=url, headers=headers)
print(response.text)
但是,这不起作用。我收到一条错误消息:
{"message": "405: Method Not Allowed", "code": 0}
在OAuth2文档上,它说如果用户成功加入,我想得到201
的响应,或者如果用户已经在行会中则得到204
的响应。
更新1 :
我将方法更改为requests.get
,现在收到此错误:
{"message": "401: Unauthorized", "code": 0}
更新2 : 我创建了一个Bot,将其邀请到不和谐的公会,并成功地获得了有关该公会用户的一些信息。但是,一旦我离开并尝试再次运行链接,就会收到此错误
{"message": "Unknown Member", "code": 10007}
更新3 : 我将方法更改为PUT,现在收到了错误请求
def add_to_guild(access_token, userID):
url = f"{Oauth.discord_api_url}/guilds/{guildid i cant show}/members/{userID}"
botToken = "cant show also"
headers = {
"Authorization" : f"Bot {botToken}",
'Content-Type': 'application/json'
}
response = requests.put(url=url, headers=headers)
print(response.text)
答案 0 :(得分:0)
要添加成员,您应该使用requests.put()
,并且Guilds Auth标头必须是Bot
令牌,因此请更改为:
headers = {
"Authorization" : f"Bot {access_token}"
}
并确保您传递的是Bot令牌。更多信息,请访问:https://discord.com/developers/docs/reference
授权标头必须是Bot令牌(属于同一令牌) 用于授权的应用程序),并且该漫游器必须是 公会获得CREATE_INSTANT_INVITE权限。
要获取该成员,可以使用不带requests.get()
标头的bot
。
答案 1 :(得分:0)
更新 3 put() 是要走的路 但是,您仍然缺少 JSON 有效负载,它必须包含从代码授予的令牌交换中收到的用户访问令牌。
data = {
"access_token" : access_token
}
然后通过将数据传递给 json 来触发您的请求,所以
response = requests.put(url=url, json=data, headers=headers)
print(response.json)