无法使用 xml rpc 通过 java 客户端创建 Drupal 用户
我有这段代码来创建一个用户,灵感来自this。
执行后我出现这个错误:
尝试创建新用户时发生错误
用户名字段为必填项。
电子邮件地址字段为必填项。
密码字段为必填项。
线程“main”org.apache.xmlrpc.XmlRpcException 中出现异常:需要用户名字段。
电子邮件地址字段为必填项。
这是我的代码:
public boolean createUser(DrupalAccount account) throws Exception
{
Vector<Object> params = generateDefaultParams(MethodUserCreate);
// code can be re-factored to use ArrayList<Object>
//ArrayList<Object> params = generateDefaultParams(MethodUserCreate);
// must also re-factor generateDefaultParams method
// currently objects don't work for submitting parameters to the
// user.save service. Use an array instead
params.add(account);
try
{
Object o = xmlRpcClient.execute(MethodUserCreate, params);
if (log.isLoggable(Level.FINEST))
{
log.finest(MethodUserCreate + " returned " + o.toString());
System.out.println(o.toString());
}
}
catch(Exception e)
{
System.err.println("An error occurred when attempting to create a user");
System.err.println(e.getMessage());
throw e;
}
//TODO add code to inform if user successfully created
return true;
}
编辑: 我尝试了 MathRo 的建议,但它仍然给出错误:
Exception in thread "main" org.apache.xmlrpc.XmlRpcException: Server error. Requested method user.create not specified. at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:197) at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156) at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143) at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69) at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126) at DrupalService.userCreate(DrupalService.java:284) at Main.main(Main.java:29)
尽管我在我的 Service 类中添加了以下内容:
public HashMap userCreate(String name, String mail, String pass)
throws XmlRpcException {
HashMap paramUser = new HashMap();
paramUser.put("name", name);
paramUser.put("mail",mail);
paramUser.put("pass",pass);
Object[] param = {paramUser};
HashMap ret_createUser = (HashMap)xmlRpcClient.execute("user.create", param);
return ret_createUser;
}
在我的主课中:
service.userCreate("myname", "[email protected]", "mypass");
I have this code to create a user, inspired by this.
After executing I have this error:
An error occurred when attempting to create a new user
Username field is required.
E-mail address field is required.
Password field is required.
Exception in thread "main" org.apache.xmlrpc.XmlRpcException: Username field is required.
E-mail address field is required.
Here is my code:
public boolean createUser(DrupalAccount account) throws Exception
{
Vector<Object> params = generateDefaultParams(MethodUserCreate);
// code can be re-factored to use ArrayList<Object>
//ArrayList<Object> params = generateDefaultParams(MethodUserCreate);
// must also re-factor generateDefaultParams method
// currently objects don't work for submitting parameters to the
// user.save service. Use an array instead
params.add(account);
try
{
Object o = xmlRpcClient.execute(MethodUserCreate, params);
if (log.isLoggable(Level.FINEST))
{
log.finest(MethodUserCreate + " returned " + o.toString());
System.out.println(o.toString());
}
}
catch(Exception e)
{
System.err.println("An error occurred when attempting to create a user");
System.err.println(e.getMessage());
throw e;
}
//TODO add code to inform if user successfully created
return true;
}
Edit: I tried MathRo's suggestion and it still gives the error:
Exception in thread "main" org.apache.xmlrpc.XmlRpcException: Server error. Requested method user.create not specified. at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:197) at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156) at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143) at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69) at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137) at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126) at DrupalService.userCreate(DrupalService.java:284) at Main.main(Main.java:29)
although I added in my Service class this:
public HashMap userCreate(String name, String mail, String pass)
throws XmlRpcException {
HashMap paramUser = new HashMap();
paramUser.put("name", name);
paramUser.put("mail",mail);
paramUser.put("pass",pass);
Object[] param = {paramUser};
HashMap ret_createUser = (HashMap)xmlRpcClient.execute("user.create", param);
return ret_createUser;
}
and in my Main class this:
service.userCreate("myname", "[email protected]", "mypass");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我不习惯 Drupal 6 和 services-6,但我会尽力提供帮助(我正在使用 D7)。
您是否尝试通过自己创建参数来调用 user.save 方法(不使用 DruaplAccount 对象)?
例如,对于我的 JavaClient(在 D7 上使用服务 3.x),我创建了一个创建用户的函数:
我不知道它是否非常有帮助,但我发布了以防万一。
Ok, I'm not used to Drupal 6 and services-6 but I'll try to help (I'm working with D7).
Did you try to call the user.save method by creating the parameter yourself (without using the DruaplAccount Object)?
For example for my JavaClient (with services 3.x on D7), I made a function to create a user:
I don't know if it's very helpful, but I posted in case it could.