Getting “$XMPPErrorException: XMPPError: forbidden - auth” error while creating MultiUserChat

点点圈 提交于 2019-12-09 03:22:59

问题


I've created a login connection succesfully for XMPP with Smack Api(4.1.4). Now i'm trying to create MultiUserChat using,

    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());

But while creating the group i'm getting an exception as

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

Hope so, this exception occurs at the code

   muc.sendConfigurationForm(submitForm);

which is commented in this for that reason. Because i didn't get the log after that code. To fix that, I have changed that code to

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

which fixes that exception and created me a group, because i could see the log as printed and can see my group in the open fire. But i do know how my selected users for the group gets added, by doing like this, since owners list(or submit form) is not included in this at anywhere. I don't know what's happening in this and I'm not sure that i'm doing right. Plz suggest me to how to proceed. Thanks in advance.


回答1:


Try this code:

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 );

Right now, if all it's ok, you'll join the Room as userName and userName will be also the owner.

You'll can check Owners of a MUC programmatically by

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

You can invite people by this line of code:

muc.invite(user, "Invite");

And then, if you want to see them "forever",

muc.grantMembership(user);

so you'll be able to see membership with

muc.getMembers();

Please pay attention: Affiliate: user with a defined role (Onwer, Admin, Member, Outcast) in a MUC Occupants: user ONLINE in a MUC

Not all Occupants can have a role, not all Affiliates are automatically be occupants.

More, you can't be sure an Affiliate ever joined the groupchat.

Flux it's something like:

Muc Creation by User1 (optional) Muc Invitation by User1 to any user he desire (example: User2, User4) (optional) Muc Affiliate assignation by User1 to any existant user he desire (example: User3, User4)

User2 and User4 will recive, when online, a invitation to accept/reject User3 and User4 will not recive nothing, but they will have a role in the MUC.

User2, User3, User4 need to register IQProviders to get IQ Stanzas and then listners for each MUC to recive Invitations, another to recive Messages (and or other events).



来源:https://stackoverflow.com/questions/37875539/getting-xmpperrorexception-xmpperror-forbidden-auth-error-while-creating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!