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

grh*_*stl 5 python rest moodle python-requests 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 last argument in the function (currently data=)
###I feel like at this point I've just been throwing random data at it and hoping something sticks haha.
r=requests.post(target, params=url_payload, data=payload)
Run Code Online (Sandbox Code Playgroud)

这是该网站的文档

Moodle API 总体结构

Moodle api XML-RPC(PHP 结构)

Moodle api REST(POST 参数)

Moodle 响应格式 1

Moodle 响应格式 2

最后是php中的例子。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>V6</title>
</head>

<body>

<?php
//load curl.php
require_once('curl.php');

function randomPassword() //according to Moodle password requirements
{
    $part1 = "";
    $part2 = "";
    $part3 = "";

    //alphanumeric LOWER
    $alphabet = "abcdefghijklmnopqrstuwxyz";
    $password_created = array(); //remember to declare $pass as an array
    $alphabetLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 3; $i++) 
    {
        $pos = rand(0, $alphabetLength); // rand(int $min , int $max)
        $password_created[] = $alphabet[$pos];
    }
    $part1 = implode($password_created); //turn the array into a string
    //echo"<br/>part1 = $part1";

    //alphanumeric UPPER
    $alphabet = "ABCDEFGHIJKLMNOPQRSTUWXYZ";
    $password_created = array(); //remember to declare $pass as an array
    $alphabetLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 3; $i++) 
    {
        $pos = rand(0, $alphabetLength); // rand(int $min , int $max)
        $password_created[] = $alphabet[$pos];
    }   
    $part2 = implode($password_created); //turn the array into a string
    //echo"<br/>part2 = $part2";

    //alphanumeric NUMBER
    $alphabet = "0123456789";
    $password_created = array(); //remember to declare $pass as an array
    $alphabetLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 2; $i++) 
    {
        $pos = rand(0, $alphabetLength); // rand(int $min , int $max)
        $password_created[] = $alphabet[$pos];
    }   
    $part3 = implode($password_created); //turn the array into a string
    //echo"<br/>part3 = $part3";

    $password = $part1 . $part2 . $part3 . "#";

    return $password;
}

function getCDate()
{
    $format = "Ymd";
    $fulldate = date($format);  
    //echo"<br/>fulldate = $fulldate";
    return $fulldate;
}

function enrol($user_id, $course_id) 
{
    $role_id = 5; //assign role to be Student

    $domainname = 'http://www.yoursite.eu'; //paste your domain here
    $wstoken = '8486ed14f3ghjec8967a0229d0a28zzz'; //here paste your enrol token 
    $wsfunctionname = 'enrol_manual_enrol_users';

    $enrolment = array( 'roleid' => $role_id, 'userid' => $user_id, 'courseid' => $course_id );
    $enrolments = array($enrolment);
    $params = array( 'enrolments' => $enrolments );

    header('Content-Type: text/plain');
    $serverurl = $domainname . "/webservice/rest/server.php?wstoken=" . $wstoken . "&wsfunction=" . $wsfunctionname;
    $curl = new curl;
    $restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
    $resp = $curl->post($serverurl . $restformat, $params);
    print_r($resp);
}

function getUserDetails()
{
    $firstname  = "TestUser";
    $lastname   = "TestUser";
    $email      = "TestUser@zzz.gr";
    $city       = "Thessaloniki";
    $country    = "EL";
    $description= "ZZZ";

    //assign username
    //get first two letters of name and surname
    //$strlength_user = strlen($firstname);
    //$strlength_pass = strlen($lastname);
    $rest_firstname = substr($firstname, 0, 2);
    $rest_lastname  = substr($lastname, 0, 2);
    $part1 = $rest_firstname . $rest_lastname;
    $part1 = strtolower($part1);
    //echo"<br/>part1 = $part1";
    $dt = getCDate();
    $part2 = substr($dt, -4);
    //echo"<br/>part2 = $part2";

    $username = $part1 . "." . $part2;
    echo"<br/>Username = $username";

    //assign password
    $password = randomPassword();
    echo"<br/>Password = $password";

    //call WS core_user_create_user of moodle to store the new user
    $domainname = 'http://www.yoursite.eu';
    $wstoken = 'ed1f6d3ebadg372f95f28cd96bd43zzz'; //here paste your create user token 
    $wsfunctionname = 'core_user_create_users';
    //REST return value
    $restformat = 'xml'; 
    //parameters
    $user1 = new stdClass();
    $user1->username    = $username;
    $user1->password    = $password;
    $user1->firstname   = $firstname;
    $user1->lastname    = $lastname;
    $user1->email       = $email;
    $user1->auth        = 'manual';
    $user1->idnumber    = 'numberID';
    $user1->lang        = 'en';
    $user1->city        = $city;
    $user1->country     = $country;
    $user1->description = $description;

    $users = array($user1);
    $params = array('users' => $users);
    //REST call
    header('Content-Type: text/plain');
    $serverurl = $domainname . "/webservice/rest/server.php?wstoken=" . $wstoken . "&wsfunction=" . $wsfunctionname;
    $curl = new curl;
    $restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
    $resp = $curl->post($serverurl . $restformat, $params);
    print_r($resp);\


    //get id from $resp
    $xml_tree = new SimpleXMLElement($resp);
    print_r($xml_tree);         
    $value = $xml_tree->MULTIPLE->SINGLE->KEY->VALUE;
    $user_id = intval(sprintf("%s",$value));
    echo"<br/>user_id number = $user_id";

    //enrol_manual_enrol_users 
    //for($i = 64; $i < 70; $i++) //where 64,65,66,67,68,69 are the six ids of the six courses of phase 1
    for($i = 64; $i < 65; $i++)
    {
        echo "\nThe user has been successfully enrolled to course " . $i;
        $course_id = $i;
        enrol($user_id, $course_id);
    }   
}

getUserDetails();

?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

grh*_*stl 0

好吧,我找到了一个可行的解决方案,但我怀疑它有点大杂烩,并且没有充分利用请求库。

我所做的是将所有参数作为 url 中的参数传递。

target = 'http://example.com/moodle/webservice/rest/server.php'
moodle_create_token = 'xxx'

payload = {
    "wstoken":moodle_create_token,
    "moodlewsrestformat":"json", #just to get response as json
    "wsfunction":"core_user_create_users",
    "users[0][username]":"testusername",
    "users[0][password]":'testpassword',
    "users[0][firstname]":'testfirstname',
    "users[0][lastname]":'testlastname',
    "users[0][email]":"testemail@example.com",
    "users[0][idnumber]":"0000001"
    }

r=requests.post(target, params=payload)
Run Code Online (Sandbox Code Playgroud)

显然,我通常不会将数据硬编码为字符串,但显然 url 参数的字典列表将是。