Problems with connecting to Facebook XMMP MD5-DIGEST

后端 未结 2 1923
一整个雨季
一整个雨季 2020-12-16 08:23

I have tried all the things to connect Facebook with XMPP but i have faced only one error all the time which is :
SASL authentication failed using mechanism D

2条回答
  •  春和景丽
    2020-12-16 09:06

    I solved this problem. I find solution that http://community.igniterealtime.org/thread/41080

    Jerry Magill wrote this...

    import java.io.IOException;
    import java.util.HashMap;
    import javax.security.auth.callback.CallbackHandler;
    import javax.security.sasl.Sasl;
    import org.jivesoftware.smack.SASLAuthentication;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.sasl.SASLMechanism;
    import org.jivesoftware.smack.packet.Packet;
    import org.jivesoftware.smack.util.Base64;
    
    public class MySASLDigestMD5Mechanism extends SASLMechanism
    {
        public MySASLDigestMD5Mechanism(SASLAuthentication saslAuthentication)
        {
            super(saslAuthentication);
        }
    
        protected void authenticate()
            throws IOException, XMPPException
        {
            String mechanisms[] = {
                getName()
            };
            java.util.Map props = new HashMap();
            sc = Sasl.createSaslClient(mechanisms, null, "xmpp", hostname, props, this);
            super.authenticate();
        }
    
        public void authenticate(String username, String host, String password)
        throws IOException, XMPPException
        {
            authenticationId = username;
            this.password = password;
            hostname = host;
            String mechanisms[] = {
                getName()
            };
            java.util.Map props = new HashMap();
            sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
            super.authenticate();
        }
    
        public void authenticate(String username, String host, CallbackHandler cbh)
            throws IOException, XMPPException
        {
            String mechanisms[] = {
                getName()
            };
            java.util.Map props = new HashMap();
            sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
            super.authenticate();
        }
    
        protected String getName()
        {
            return "DIGEST-MD5";
        }
    
        public void challengeReceived(String challenge)
            throws IOException
        {
            //StringBuilder stanza = new StringBuilder();
            byte response[];
            if(challenge != null)
                response = sc.evaluateChallenge(Base64.decode(challenge));
            else
                //response = sc.evaluateChallenge(null);
                response = sc.evaluateChallenge(new byte[0]);
            //String authenticationText = "";
            Packet responseStanza;
            //if(response != null)
            //{
                //authenticationText = Base64.encodeBytes(response, 8);
                //if(authenticationText.equals(""))
                    //authenticationText = "=";
    
            if (response == null){
                responseStanza = new Response();
            } else {
                responseStanza = new  Response(Base64.encodeBytes(response,Base64.DONT_BREAK_LINES));   
            }
            //}
            //stanza.append("");
            //stanza.append(authenticationText);
            //stanza.append("");
            //getSASLAuthentication().send(stanza.toString());
            getSASLAuthentication().send(responseStanza);
        }
    }
    

    It is then called thus from the JabberSmackAPI:

    public void login(String userName, String password) throws XMPPException
    {       
        SASLAuthentication.registerSASLMechanism("DIGEST-MD5",MySASLDigestMD5Mechanism. class);
        ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222);
    
        config.setSASLAuthenticationEnabled(true);
        config.setRosterLoadedAtLogin (true);
    
        connection = new XMPPConnection(config);
        connection.connect();
        connection.login(userName, password);
    }
    

提交回复
热议问题