[英]moodle api rest call in c#
I'm doing the next api rest call in c#: 我正在用C#进行下一个api rest调用:
String token = "e2a35dbfdaee78097c7ba489xxxxxxxx";
MoodleUser user = new MoodleUser();
user.username = WebUtility.UrlEncode("username");
user.password = WebUtility.UrlEncode("the_password");
user.firstname = WebUtility.UrlEncode("Michael");
user.lastname = WebUtility.UrlEncode("York");
user.email = WebUtility.UrlEncode("test@gmail.com");
List<MoodleUser> userList = new List<MoodleUser>();
userList.Add(user);
Array arrUsers = userList.ToArray();
String postData = String.Format(@"users[0][username]={0}&users[0][password]={1}&users[0][firstname]={2}&users[0][lastname]={3}&users[0][email]={4}&users[0][preferences][0][type]={5}&users[0][preferences][0][value]={6}", user.username, user.password, user.firstname, user.lastname, user.email, **"auth_forcepasswordchange", "1");**
string createRequest = string.Format("http://domain.es/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", token, "core_user_create_users");
// Call Moodle REST Service
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(createRequest);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
// Encode the parameters as form data:
byte[] formData = UTF8Encoding.UTF8.GetBytes(postData);
req.ContentLength = formData.Length;
// Write out the form Data to the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
....
.....
The problem occurs when call the api with the preferences array auth_forcepasswordchange, the result of the errror is "invalid_parameter_exception". 当使用首选项数组auth_forcepasswordchange调用api时,会出现问题,错误的结果是“ invalid_parameter_exception”。
When I call without the preferences parameter, works perfectly. 当我不使用preferences参数调用时,效果很好。
Tank you very much. 给你加油
The code below will work for getting course information. 以下代码可用于获取课程信息。 I generated the token in Moodle and then just use the token in the API call 我在Moodle中生成了令牌,然后在API调用中使用了令牌
string result = "";
string methodName = "GetCourse";
string apiName = "core_course_get_courses";
string apiCall = moodleUrl + "?wstoken=" + token + "&wsfunction=" + apiName + "&moodlewsrestformat=json";
try
{
using (WebClient client = new WebClient())
{
client.BaseAddress = apiCall;
client.Headers[HttpRequestHeader.Accept] = "application/json";
result = client.DownloadString(apiCall);
ValidateJson validate = new ValidateJson();
}
}
catch(Exception ex)
{
//print error here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.