【问题标题】:How to create a user in the Moodle Rest WS using the Python Requests module?如何使用 Python Requests 模块在 Moodle Rest WS 中创建用户?
【发布时间】:2016-07-22 09:14:06
【问题描述】:

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

import requests

token = 'TOKENNUMBER'
function = 'core_user_create_users'


url = 'http://localhost/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsformat=json'.format(token,function)

user1 = {'email': 'email@local.host','firstname': 'firstname',
'lastname': 'lastname', 'createpassword': 1,
'username': 'username'}

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

requests.post(url,data={'users': user1})
requests.post(url,data={'users': [user1,]})

并且moodle保持返回错误:

Only arrays accepted. The bad value is: \'username\'</DEBUGINFO>

在文档中(可从自己的 Moodle 中获得),其声明:

Argumentos
users (Obrigatório)


Estrutura geral

list of ( 
object {
username string   //Username policy is defined in Moodle security config.
password string  Opcional //Plain text password consisting of any characters
createpassword int  Opcional //True if password should be created and mailed to user.
firstname string   //The first name(s) of the user
lastname string   //The family name of the user
email string   //A valid and unique email address
auth string  Padrão para "manual" //Auth plugins include manual, ldap, imap, etc
idnumber string  Padrão para "" //An arbitrary ID code number perhaps from the institution
lang string  Padrão para "pt_br" //Language code such as "en", must exist on server
calendartype string  Padrão para "gregorian" //Calendar type such as "gregorian", must exist on server
theme string  Opcional //Theme name such as "standard", must exist on server
timezone string  Opcional //Timezone code such as Australia/Perth, or 99 for default
mailformat int  Opcional //Mail format code is 0 for plain text, 1 for HTML etc
description string  Opcional //User profile description, no HTML
city string  Opcional //Home city of the user
country string  Opcional //Home country code of the user, such as AU or CZ
firstnamephonetic string  Opcional //The first name(s) phonetically of the user
lastnamephonetic string  Opcional //The family name phonetically of the user
middlename string  Opcional //The middle name of the user
alternatename string  Opcional //The alternate name of the user
preferences  Opcional //User preferences
list of ( 
object {
type string   //The name of the preference
value string   //The value of the preference
} 
)customfields  Opcional //User custom fields (also known as user profil fields)
list of ( 
object {
type string   //The name of the custom field
value string   //The value of the custom field
} 
)} 
)

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

【问题讨论】:

    标签: python web-services rest python-requests moodle


    【解决方案1】:

    要在 Moodle 中使用 REST 服务,函数的参数必须在平面字典中格式化。参数的结构反映在键的名称中。在您的示例中,您有一个参数 courses 是一个列表。因此,您的字典的键将是 courses[0]emailcourses[0]firstname、... 对于第一个用户,以及 courses[1]email , courses[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)
    

    【讨论】:

    • 感谢您的回答。对我来说,格式真的很奇怪......似乎他们正在使用数组列或类似的东西来获取索引......
    • 似乎在较新的 Moodle 版本中它发生了一些变化。对我有用的是 {'users[0][email]': 'email@local.host' , ...} ,在键中添加括号。
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多