使用Discord Oauth2加入公会
import requests
API_ENDPOINT = 'https://discord.com/api/v8'
CLIENT_ID = 'client id here'
CLIENT_SECRET = 'client secret here'
REDIRECT_URI = "https://google.com"
def exchange_code(code):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
r.raise_for_status()
return r.json()
def add_to_guild(access_token, userID, guildID):
url = f"{API_ENDPOINT}/guilds/{guildID}/members/{userID}"
botToken = "bot token here"
data = {
"access_token": access_token,
}
headers = {
"Authorization": f"Bot {botToken}",
'Content-Type': 'application/json'
}
response = requests.put(url=url, headers=headers, json=data)
print(response.text)
code = exchange_code('code here')['access_token']
add_to_guild(code, 'user id here', 'guild id here')
ifii取出第二个功能,以下是没有错误,但是当我添加add_to_guild函数时,我总是会收到以下错误,我花了几个小时来研究修复程序,但是无论我做什么,我都会得到相同的错误。
Traceback (most recent call last):
File "main.py", line 55, in <module>
code = exchange_code('JZe2wsCPNxZwH6K7UJq130xmAu22xW')['access_token']
File "main.py", line 22, in exchange_code
r.raise_for_status()
File "/home/runner/RelevantLooseRecovery/venv/lib/python3.8/site-packages/requests/models.py", line 1022, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://discord.com/api/v8/oauth2/token
如果有人能提供帮助,我真的很感激!
import requests
API_ENDPOINT = 'https://discord.com/api/v8'
CLIENT_ID = 'client id here'
CLIENT_SECRET = 'client secret here'
REDIRECT_URI = "https://google.com"
def exchange_code(code):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
r.raise_for_status()
return r.json()
def add_to_guild(access_token, userID, guildID):
url = f"{API_ENDPOINT}/guilds/{guildID}/members/{userID}"
botToken = "bot token here"
data = {
"access_token": access_token,
}
headers = {
"Authorization": f"Bot {botToken}",
'Content-Type': 'application/json'
}
response = requests.put(url=url, headers=headers, json=data)
print(response.text)
code = exchange_code('code here')['access_token']
add_to_guild(code, 'user id here', 'guild id here')
IfIi take out the second function and below, i get no errors but when I add the add_to_guild function in, I always get the following error, I have spent hours researching for a fix but whatever I do I get the same error.
Traceback (most recent call last):
File "main.py", line 55, in <module>
code = exchange_code('JZe2wsCPNxZwH6K7UJq130xmAu22xW')['access_token']
File "main.py", line 22, in exchange_code
r.raise_for_status()
File "/home/runner/RelevantLooseRecovery/venv/lib/python3.8/site-packages/requests/models.py", line 1022, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://discord.com/api/v8/oauth2/token
If anyone could help I would really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论