moodle api rest call in c# -
i'm doing next api rest call in c#:
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 parameters form data: byte[] formdata = utf8encoding.utf8.getbytes(postdata); req.contentlength = formdata.length; // write out form data request: using (stream post = req.getrequeststream()) { post.write(formdata, 0, formdata.length); } .... .....
the problem occurs when call api preferences array auth_forcepasswordchange, result of errror "invalid_parameter_exception".
when call without preferences parameter, works perfectly.
tank much.
the code below work getting course information. generated token in moodle , use token in api call
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();
Comments
Post a Comment