【问题标题】:Getting "$XMPPErrorException: XMPPError: forbidden - auth" error while creating MultiUserChat创建 MultiUserChat 时出现“$XMPPErrorException:XMPPError:禁止 - auth”错误
【发布时间】:2016-10-18 22:27:37
【问题描述】:

我已经使用 Smack Api(4.1.4) 成功为 XMPP 创建了登录连接。现在我正在尝试使用创建 MultiUserChat,

    try {
        String myMUCName = "TestGroup";
        String myMUCService = "conference.(my local ip)";
        String myMUCfullName = myMUCName + "@" + myMUCService;
        String userName =  "Test5";

        MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
        MultiUserChat muc = manager.getMultiUserChat(myMUCfullName);
        muc.create(userName);

        Log.d(LOCAL_TAG, "createGroupChat  -- Group CEATED Successfully ");
        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();

        List<FormField> fields = form.getFields();
        Log.d(LOCAL_TAG, "createGroupChat  -- fields.size(): "+fields.size());
        for (int i = 0; i < fields.size(); i++) {
            FormField field = (FormField) fields.get(i);
            if (!FormField.Type.hidden.equals(field.getType()) && field.getVariable() != null) {
                submitForm.setDefaultAnswer(field.getVariable());
            }
        }

        List owners = new ArrayList();
        owners.add(userName); //Own user
        owners.add("Test7"); //Another user

        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        submitForm.setAnswer("muc#roomconfig_publicroom", true);
        submitForm.setAnswer("muc#roomconfig_persistentroom", true);
        muc.sendConfigurationForm(new Form(DataForm.Type.submit));
        //muc.sendConfigurationForm(submitForm);
    Log.d(LOCAL_TAG, "createGroupChat  -- Sent Configuration");
        muc.join(TestGroup);
        Log.d(LOCAL_TAG, "createGroupChat  -- Group Joined Successfully -- owners.size(): "+owners.size());

但是在创建组时我遇到了一个例外

    "org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth". 

希望如此,这个异常发生在代码中

   muc.sendConfigurationForm(submitForm);

出于这个原因,对此进行了评论。因为在那段代码之后我没有得到日志。为了解决这个问题,我已将该代码更改为

   muc.sendConfigurationForm(new Form(DataForm.Type.submit));

它修复了该异常并为我创建了一个组,因为我可以看到打印的日志并且可以看到我的组在开火中。但是我确实知道如何通过这样做添加我为该组选择的用户,因为所有者列表(或提交表单)在任何地方都不包含在其中。我不知道发生了什么,我不确定我做对了。请建议我如何进行。提前致谢。

【问题讨论】:

  • 嗨@Sangeetha 我也遇到了同样的问题,你能描述一下你是如何解决这个错误的吗?

标签: android exception smack


【解决方案1】:

试试这个代码:

Form form = muc.getConfigurationForm().createAnswerForm();


        // Create a new form to submit based on the original form
        form.setAnswer("muc#roomconfig_passwordprotectedroom", false);
        form.setAnswer("muc#roomconfig_roomname",myMUCName);
        form.setAnswer("muc#roomconfig_persistentroom", true);
        form.setAnswer("muc#roomconfig_changesubject", true);
        form.setAnswer("muc#roomconfig_publicroom",true);
        form.setAnswer("muc#roomconfig_allowinvites",true);
        form.setAnswer("muc#roomconfig_membersonly",false);
        form.setAnswer("muc#roomconfig_moderatedroom",false);

        // Sets the new owner of the room
        List<String> owners = new ArrayList<String>();

        //Be carefull: if members does not exists, it brokes!

        owners.add(userName +"@"+"(my local ip or server name placeholder)");
        form.setAnswer("muc#roomconfig_roomowners", owners);

        // Send the completed form
        muc.sendConfigurationForm(form);
        System.out.println("MUC is now registered");

        muc.join(userName );

现在,如果一切正常,您将以 userName 的身份加入 Room,并且 userName 也将成为所有者。

您可以通过以下方式以编程方式检查 MUC 的所有者

muc.getOwners()  //List<Affiliate>, for each Affialiate you'll have to affiliate.getJid().toString()

你可以通过这行代码邀请人:

muc.invite(user, "Invite");

然后,如果你想“永远”看到它们,

muc.grantMembership(user);

这样您就可以看到会员资格

muc.getMembers();

请注意: Affiliate:在 MUC 中具有已定义角色(Onwer、Admin、Member、Outcast)的用户 居住者:MUC 中的用户 ONLINE

并非所有占用者都可以担任角色,并非所有附属公司都自动成为占用者。

此外,您无法确定会员是否加入了群聊。

Flux 类似于:

由 User1 创建的 Muc (可选)用户 1 向他想要的任何用户发出 Muc 邀请(例如:用户 2、用户 4) (可选)由 User1 将 Muc Affiliate 分配给他想要的任何现有用户(例如:User3、User4)

User2 和 User4 在线时会收到接受/拒绝的邀请 User3 和 User4 不会收到任何信息,但他们将在 MUC 中发挥作用。

User2、User3、User4 需要注册 IQProviders 以获取 IQ 节,然后每个 MUC 的侦听器接收邀请,另一个接收消息(和/或其他事件)。

【讨论】:

  • 嗨.. 非常感谢您的回复。它修复了该异常。但是,当我尝试使用该代码邀请用户时,即使第二个用户在线,我也没有收到其他用户的任何邀请通知。我邀请第二个用户为 "muc.invite("test7@(domain name)", "Meet me in this good room"); muc.grantMembership("test7@(domain name)"); muc.getMembers( );"。如何检查我是否已发送邀请以及其他用户是否已收到该邀请。
  • 在 muc 代码中不需要添加 domain.name,但请务必注册 InvitationListener (MultiUserChatManager.addInvitationListener(new InvitationListner)。我还建议在 javadoc 上查看这些类;)
  • 嗨@Sangeetha 我也遇到了同样的问题,你能描述一下你是如何解决这个错误的吗?
【解决方案2】:

适用于 SMACK 4.3.4 及更高版本。

        multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);

        multiUserChat = multiUserChatManager.getMultiUserChat(JidCreate.entityBareFrom(roomJID));

        multiUserChat.create(Resourcepart.from(nickname));

        Form form = multiUserChat.getConfigurationForm();
        Form submitForm = form.createAnswerForm(); submitForm.getField("muc#roomconfig_publicroom").addValue("1");
        submitForm.getField("muc#roomconfig_enablelogging").addValue("1");
        submitForm.getField("x-muc#roomconfig_reservednick").addValue("0");
        submitForm.getField("x-muc#roomconfig_canchangenick").addValue("0");
        submitForm.getField("x-muc#roomconfig_registration").addValue("0");
        submitForm.getField("muc#roomconfig_passwordprotectedroom").addValue("0");
        submitForm.getField("muc#roomconfig_roomname").addValue(roomName);
        submitForm.getField("muc#roomconfig_whois").addValue("participants");
        submitForm.getField("muc#roomconfig_membersonly").addValue("1");
        submitForm.getField("muc#roomconfig_persistentroom").addValue("1");
        multiUserChat.sendConfigurationForm(submitForm);

这是发送房间配置和创建房间 (MUC) 的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2013-07-19
    相关资源
    最近更新 更多