如何使用 Python 请求模块在 Moodle Rest WS 中创建用户?

art*_*_dk 4 python rest web-services moodle python-requests

我正在尝试使用 Moodle Webservices - Rest Server 创建一个用户,但我陷入了参数验证:S 我的代码如下:

\n\n
import requests\n\ntoken = \'TOKENNUMBER\'\nfunction = \'core_user_create_users\'\n\n\nurl = \'http://localhost/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsformat=json\'.format(token,function)\n\nuser1 = {\'email\': \'email@local.host\',\'firstname\': \'firstname\',\n\'lastname\': \'lastname\', \'createpassword\': 1,\n\'username\': \'username\'}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,我尝试发布数据(两种不同的方式):

\n\n
requests.post(url,data={\'users\': user1})\nrequests.post(url,data={\'users\': [user1,]})\n
Run Code Online (Sandbox Code Playgroud)\n\n

Moodle 保持返回错误:

\n\n
Only arrays accepted. The bad value is: \\\'username\\\'</DEBUGINFO>\n
Run Code Online (Sandbox Code Playgroud)\n\n

在文档(可从自己的moodle获取)中,它指出:

\n\n
Argumentos\nusers (Obrigat\xc3\xb3rio)\n\n\nEstrutura geral\n\nlist of ( \nobject {\nusername string   //Username policy is defined in Moodle security config.\npassword string  Opcional //Plain text password consisting of any characters\ncreatepassword int  Opcional //True if password should be created and mailed to user.\nfirstname string   //The first name(s) of the user\nlastname string   //The family name of the user\nemail string   //A valid and unique email address\nauth string  Padr\xc3\xa3o para "manual" //Auth plugins include manual, ldap, imap, etc\nidnumber string  Padr\xc3\xa3o para "" //An arbitrary ID code number perhaps from the institution\nlang string  Padr\xc3\xa3o para "pt_br" //Language code such as "en", must exist on server\ncalendartype string  Padr\xc3\xa3o para "gregorian" //Calendar type such as "gregorian", must exist on server\ntheme string  Opcional //Theme name such as "standard", must exist on server\ntimezone string  Opcional //Timezone code such as Australia/Perth, or 99 for default\nmailformat int  Opcional //Mail format code is 0 for plain text, 1 for HTML etc\ndescription string  Opcional //User profile description, no HTML\ncity string  Opcional //Home city of the user\ncountry string  Opcional //Home country code of the user, such as AU or CZ\nfirstnamephonetic string  Opcional //The first name(s) phonetically of the user\nlastnamephonetic string  Opcional //The family name phonetically of the user\nmiddlename string  Opcional //The middle name of the user\nalternatename string  Opcional //The alternate name of the user\npreferences  Opcional //User preferences\nlist of ( \nobject {\ntype string   //The name of the preference\nvalue string   //The value of the preference\n} \n)customfields  Opcional //User custom fields (also known as user profil fields)\nlist of ( \nobject {\ntype string   //The name of the custom field\nvalue string   //The value of the custom field\n} \n)} \n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

那么,考虑到这一点,如何使用 python requests 模块创建 Moodle 用户?传过来的数据有什么问题吗?

\n

mrc*_*cin 5

要在 Moodle 中使用 REST 服务,函数参数必须以平面字典的形式格式化。参数的结构反映在键的名称中。在您的示例中,您有一个参数课程,它是一个列表。因此,您的字典的键将是course[0]emailcourse[0]firstname、... 对于第一个用户,以及 course[1]emailcourse[1]firstname、... 对于第二个用户,依此类推

users = {'users[0]email': 'email@local.host',
         'users[0]firstname': 'firstname',
         'users[0]lastname': 'lastname', 
         'users[0]createpassword': 1,
         'users[0]username': 'username'}
requests.post(url,data=users)
Run Code Online (Sandbox Code Playgroud)