我正在使用Ejabberd Server和Xmpp创建聊天应用程序。在这个应用程序中,我想添加Android代码的新用户。我使用asmack-android-8-0.8.10.jar。我在stackoverflow.com上检查了关于这个问题的其他问题,我做了他们说的但是我无法解决问题......
我尝试使用此代码添加新用户,就像有人说:
HashMap<String, String> attr = new HashMap<String, String>();
attr.put("user", username);
attr.put("password", password);
try {
// Admin login
connection.login(username, password);
} catch (XMPPException e) {
e.printStackTrace();
}
Log.i("connection.isAuthenticated() : ","" + connection.isAuthenticated());
if (connection.isAuthenticated()) {
AccountManager manager = new AccountManager(connection);
try {
if (manager.supportsAccountCreation())
{
manager.createAccount(username, password, attr);
}
} catch (XMPPException e) {
Log.w("[create_user] Cannot create new user: XMPP Exception.","0");
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w("[create_user] Cannot create new user: not logged in.", "0");
e.printStackTrace();
}
}
但它给了我这个错误:
12-01 17:12:32.436: D/SMACK(16442): 05:12:32 PM SENT (1101049376): <iq id="44B8E-6" to="mehmetcan" type="set"><query xmlns="jabber:iq:register"><password>can123</password> <user>user2fasf</user><username>user2fasf</username></query></iq>
12-01 17:12:32.626: D/SMACK(16442): 05:12:32 PM RCV (1101049376): <iq from='mehmetcan' to='admin@mehmetcan/Smack' id='44B8E-6' type='error'><query xmlns='jabber:iq:register'><password>can123</password><user>user2fasf</user><username>user2fasf</username></query><error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
12-01 17:12:32.626: W/[create_user] Cannot create new user: XMPP Exception.(16442): 0
12-01 17:12:32.626: W/System.err(16442): forbidden(403)
12-01 17:12:32.626: W/System.err(16442): at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:243)
12-01 17:12:32.626: W/System.err(16442): at org.apache.android.xmppClient.XmppUserCreateTask.doInBackground(XmppUserCreateTask.java:66)
12-01 17:12:32.626: W/System.err(16442): at org.apache.android.xmppClient.XmppUserCreateTask.doInBackground(XmppUserCreateTask.java:1)
12-01 17:12:32.626: W/System.err(16442): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-01 17:12:32.626: W/System.err(16442): at java.lang.Thread.run(Thread.java:856)
然后就像人们说的那样,在ejabberd.cfg文件中,我做了这个改动:
%% Put this in the section ACCESS RULES
{access, register_from, [{allow, admin}]}.
并将mod_register更改为access_from的访问权限并注册到register_from:
{mod_register, [
%%
%% After successful registration, the user receives
%% a message with this subject and body.
%%
{welcome_message, {"Welcome!",
"Welcome to this Jabber server."}},
%%
%% When a user registers, send a notification to
%% these Jabber accounts.
%%
%%{registration_watchers, ["admin1@example.org"]},
{access_from, register_from}
]},
它仍然给我同样的错误:
Cannot create new user: XMPP Exception.(16442): 0 forbidden (403)
如何解决这个问题?这是因为ejabberd的一些更新?请帮助..
答案 0 :(得分:3)
您只允许您在&#34; admin&#34;中定义的用户注册帐户的规则。你没有发布你如何定义&#34; admin&#34;。 但是,根据您的示例,我希望您允许任何人注册帐户。也许,以下是你想要的?
{access, register_from, [{allow, all}]}.