使用Python的requests库加入Discord服务器时收到400错误代码。

huangapple go评论96阅读模式
英文:

getting 400 code while joining discord servers with discord API using requests python

问题

我的经验:

> 在开发一个使用Python和Requests模块加入Discord服务器的功能时,我一直收到错误代码:400。

我的代码:

def joiner():
    invite_code = input("输入邀请码:")
    with open('tokens.json', 'r') as file:
        tokens = json.load(file)
        tokens = tokens.get('token', [])
    for token in tokens:
        headers = {
            "Authorization": f"{token}"
        }
        url = f"https://discord.com/api/v9/invites/{invite_code}"
        if "discord.gg/" in url:
            url = url.removeprefix('discord.gg/')

        response = requests.post(url, headers=headers)
        if response.status_code == 200:
            print(f"令牌:{token} 成功加入了服务器!")
        else:
            print(f"使用令牌加入服务器失败:{token}。状态码:{response.status_code}")

追踪:

> 使用令牌加入服务器失败:<token>。状态码:400

(请注意,追踪不是100%真实的,我做了一个修改,用<token>替换了真实的令牌)

英文:

What I'm experiencing:

> While developing a function to join discord servers using Python and Requests module, I keep getting the error code : 400.

My code:

def joiner():
    invite_code = input(&quot;Enter the invite code: &quot;)
    with open(&#39;tokens.json&#39;, &#39;r&#39;) as file:
        tokens = json.load(file)
        tokens = tokens.get(&#39;token&#39;,[])
    for token in tokens:
        headers = {
            &quot;Authorization&quot;: f&quot;{token}&quot;
        }
        url = f&quot;https://discord.com/api/v9/invites/{invite_code}&quot;
        if &quot;discord.gg/&quot; in url:
            url = url.removeprefix(&#39;discord.gg/&#39;)

        response = requests.post(url, headers=headers)
        if response.status_code == 200:
            print(f&quot;Token: {token} has successfully joined the server!&quot;)
        else:
            print(f&quot;Failed to join the server with token: {token}. Status code: {response.status_code}&quot;)

Traceback:

> Failed to join the server with token: &lt;token&gt;. Status code: 400

(Please note that the traceback isn't 100% the real one, there is one modification that I made and it's replacing the real token by &lt;token&gt;)

答案1

得分: 1

在这种情况下,您应该检查Discord在response.text中返回的错误消息,例如,在底部添加:

if 400 <= resp.status_code < 500:
    print(response.text, file=sys.stderr)
英文:

In this situation, you should examine the error message that Discord returns in response.text, e.g. at the bottom, add:

if 400 &lt;= resp.status_code &lt; 500:
    print(response.text, file=sys.stderr)

huangapple
  • 本文由 发表于 2023年6月16日 06:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定