使用Python Request调用Moodle API创建用户遇invalid_parameter_exception
问题:使用Python Requests调用Moodle API注册用户时提示参数无效
我正尝试使用Python Request库通过API注册Moodle用户,代码如下:
import requests url = "www.website.com/webservice/rest/server.php?createpassword=1&username=thisissifumwike&auth=manual&password=Tester@2023.&firstname=Domain&lastname=Tester&email=hello@gmail.com&maildisplay=sifugmail&city=Daressalaam&country=Tanzania&timezone=99&description=I am a good tester&firstnamephonetic=Sifu&lastnamephonetic=Domain&middlename=Tester&alternatename=Tunety&interests=books, codes, forex&idnumber=FMM001&institution=FMM&department=Traders&phone1=0611222333&phone2=0611333222&address=Daressalaam&lang=en&calendartype=gregorian&theme=default&mailformat=1&customfields=&wstoken=token&wsfunction=core_user_create_users&moodlewsrestformat=json" payload={} headers = {} response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
发送请求后收到响应:
{ "exception": "invalid_parameter_exception", "errorcode": "invalidparameter", "message": "Invalid parameter value detected" }
请求帮忙排查参数错误原因。
排查方向
maildisplay参数值非法:Moodle的maildisplay仅接受数值,可选值为0(隐藏邮箱)、1(仅对联系人可见)、2(对所有人可见),传入的sifugmail是无效字符串,需替换为合法数值。country参数格式错误:country要求使用ISO 3166-1 alpha-2格式的国家代码(比如坦桑尼亚对应TZ),而不是国家全称Tanzania,不符合格式会触发校验失败。timezone参数值错误:timezone需要传入标准时区标识符(如Africa/Dar_es_Salaam),数字99是旧版Moodle的"未设置"标识,新版API已不支持该写法。customfields参数处理不当:无自定义字段时应省略该参数,或传入符合格式的空结构(如customfields[]=),直接传空字符串customfields=不符合API参数规范。- URL缺少协议头:请求URL开头没有
http://或https://,会导致请求无法正确寻址,先修复该基础问题。 password可能不符合站点规则:检查Moodle站点的密码策略(如长度、特殊字符组合要求),Tester@2023.末尾的点可能不被允许,或长度未达标。
内容的提问来源于stack exchange,提问作者thisissifumwike
相关产品推荐
相关产品推荐

