【问题标题】:moodle api rest call in c#C#中的moodle api休息调用
【发布时间】:2015-07-01 21:32:16
【问题描述】:

我正在用 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);
 }

....
.....

使用首选项数组auth_forcepasswordchange调用api时出现问题,错误的结果是“invalid_parameter_exception”。

当我在没有偏好参数的情况下调用时,效果很好。

非常感谢你。

【问题讨论】:

  • 某些操作需要登录用户(以及其他权限,如管理员角色)。您的其余呼叫似乎不需要 Web 服务用户登录。

标签: c# rest preferences moodle


【解决方案1】:

下面的代码将用于获取课程信息。 我在 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
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多