使用Discord Oauth2加入公会

发布于 2025-02-09 10:46:43 字数 1784 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文