gpt4 book ai didi

java - 外部应用程序使用 Java Web 服务进行 Moodle 身份验证

转载 作者:行者123 更新时间:2023-11-30 07:26:50 27 4
gpt4 key购买 nike

我正在尝试使用 java web 服务连接到 Moodle。我需要做的是验证moodle并将用户保存到其中。

任何人都可以为我提供一些用于身份验证的 java 示例代码吗?这将是一个很大的帮助。

最佳答案

基本REST MOODLE 客户端示例:

 import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

/**
* REST MOODLE Client
* It's very basic. You'll have to write the JavaObject2POST code.
*
*/
public class RestJsonMoodleClient {

/**
* Do a REST call to Moodle. Result are displayed in the console log.
* @param args the command line arguments
*/
public static void main(String[] args) throws ProtocolException, IOException {

/// NEED TO BE CHANGED
String token = "acabec9d20933913f14301785324f579";
String domainName = "http://www.yourmoodle.com";

/// REST RETURNED VALUES FORMAT
String restformat = "xml"; //Also possible in Moodle 2.2 and later: 'json'
//Setting it to 'json' will fail all calls on earlier Moodle version
if (restformat.equals("json")) {
restformat = "&moodlewsrestformat=" + restformat;
} else {
restformat = "";
}

/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
String functionName = "core_user_create_users";
String urlParameters =
"users[0][username]=" + URLEncoder.encode("testusername1", "UTF-8") +
"&users[0][password]=" + URLEncoder.encode("testpassword1", "UTF-8") +
"&users[0][firstname]=" + URLEncoder.encode("testfirstname1", "UTF-8") +
"&users[0][lastname]=" + URLEncoder.encode("testlastname1", "UTF-8") +
"&users[0][email]=" + URLEncoder.encode("testemail1@moodle.com", "UTF-8") +
"&users[0][auth]=" + URLEncoder.encode("manual", "UTF-8") +
"&users[0][idnumber]=" + URLEncoder.encode("testidnumber1", "UTF-8") +
"&users[0][lang]=" + URLEncoder.encode("en", "UTF-8") +
"&users[0][theme]=" + URLEncoder.encode("standard", "UTF-8") +
"&users[0][timezone]=" + URLEncoder.encode("-12.5", "UTF-8") +
"&users[0][mailformat]=" + URLEncoder.encode("0", "UTF-8") +
"&users[0][description]=" + URLEncoder.encode("Hello World!", "UTF-8") +
"&users[0][city]=" + URLEncoder.encode("testcity1", "UTF-8") +
"&users[0][country]=" + URLEncoder.encode("au", "UTF-8") +
"&users[0][preferences][0][type]=" + URLEncoder.encode("preference1", "UTF-8") +
"&users[0][preferences][0][value]=" + URLEncoder.encode("preferencevalue1", "UTF-8") +
"&users[0][preferences][1][type]=" + URLEncoder.encode("preference2", "UTF-8") +
"&users[0][preferences][1][value]=" + URLEncoder.encode("preferencevalue2", "UTF-8") +
"&users[1][username]=" + URLEncoder.encode("testusername2", "UTF-8") +
"&users[1][password]=" + URLEncoder.encode("testpassword2", "UTF-8") +
"&users[1][firstname]=" + URLEncoder.encode("testfirstname2", "UTF-8") +
"&users[1][lastname]=" + URLEncoder.encode("testlastname2", "UTF-8") +
"&users[1][email]=" + URLEncoder.encode("testemail2@moodle.com", "UTF-8") +
"&users[1][timezone]=" + URLEncoder.encode("Pacific/Port_Moresby", "UTF-8");

/// REST CALL

// Send request
String serverurl = domainName + "/webservice/rest/server.php" + "?wstoken=" + token + "&wsfunction=" + functionName;
HttpURLConnection con = (HttpURLConnection) new URL(serverurl).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Language", "en-US");
con.setDoOutput(true);
con.setUseCaches (false);
con.setDoInput(true);
DataOutputStream wr = new DataOutputStream (
con.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();

//Get Response
InputStream is =con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
}
}

欲了解更多信息,请点击链接:

https://docs.moodle.org/dev/Creating_a_web_service_client#Difference_between_Moodle_versions

http://www.rumours.co.nz/manuals/using_moodle_web_services.htm

https://github.com/mudrd8mz/node-moodle-client

分享并享受...:)

关于java - 外部应用程序使用 Java Web 服务进行 Moodle 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36715304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com