小编grh*_*stl的帖子

通过Python的请求库访问Moodle服务器的API

我正在尝试使用 Python 的请求库访问 Moodle 安装的 Web 服务。我有 API 的文档和一个用 php 编写的示例项目(我以前没有看过 php,它比我预期的要理解的要困难得多),但我真的很难正确格式化请求。该网站返回检测到的无效参数,因此我非常确定我的端点、授权令牌和服务器配置正在工作,只是数据的格式让我失望。

首先这里是错误...

<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="invalid_parameter_exception">
<ERRORCODE>invalidparameter</ERRORCODE>
<MESSAGE>Invalid parameter value detected</MESSAGE>
</EXCEPTION>
Run Code Online (Sandbox Code Playgroud)

现在我的代码...

import requests

target = 'http://example.com/moodle/webservice/rest/server.php?'
moodle_create_token = 'xxx'
moodle_enrol_token = 'yyy'
url_payload = {
    "wstoken":moodle_create_token,
   "wsfunction":"core_user_create_users"
    }

###not sure if I should just be passing this as a dict or some deeper more layered struct
payload = {
    "username":"testuser",
    "password":'testpass',
    "firstname":'testf',
    "lastname":'testl',
    "email":"test@example.com",
    "idnumber":"1234"
}

###not sure how to include the payload as the …
Run Code Online (Sandbox Code Playgroud)

python rest moodle python-requests moodle-api

5
推荐指数
1
解决办法
7030
查看次数

标签 统计

moodle ×1

moodle-api ×1

python ×1

python-requests ×1

rest ×1