Problems with connecting to Facebook XMMP MD5-DIGEST

may be u need to add this:

config.setSASLAuthenticationEnabled(true);

this reply is intended for PEDRO!

Yup, that definantly worked… THANK YOU VERY MUCH!!! YOU SAVED MY PROJECT

then u should have liked it ! lol! by the way what type of project is this? i worked on one a few days ago for my term completion in University!

I study art academy, Interaction Design, i’m combining a bot called Eliza(one of the first bots ever) together with facebook chat. I’m making a fake profile and let people interact with this person to check what are the results. After that i’m gonna modify the bot to an megalomaniac person and try to annoy people with it. The results i’m gonna make a book out of it.

interesting!keep it up!

I have still one problem… how do i use the getchat command then? I’ve tried an userID like this:

newChat = chatmanager.createChat(“10430122958887”, new MessageListener() {

put @chat.facebook.com after it, but it doesn’t send any messages. Tried full name. How did you do this?

EDIT:

solved! Just put in “-@chat.facebook.com” don’t forget the “-”

i almost came with the solution but u got it before…great!

if u have any problem further related to chat try this (its from my project):

private class messageProcessor implements MessageListener
{

            Chat chat;//chat variable
            public messageProcessor(String address)//constructor recieving the address of the participant
            {
                chat = connection.getChatManager().createChat(to, this);
            }
           
            public void processMessage(Chat chat, Message message)
                    {
                        try
                        {
                       
                            Thread.sleep (3000);
                        }
                        catch(Exception ex){};
                        if(message.getType() == Message.Type.chat)
                            if(message.getBody ()!=null)
                                setIncomingMessage(message.getBody());
                       
                    }
               
               
       
            public void sendMessage(String message) throws XMPPException
            {
               
                chat.sendMessage(message);
                try{Thread.sleep (600);}catch(Exception ex){};
               
            }
   }

HI ,

I tried the same thing .

Changed the smack library . Added null

sc = Sasl.createSaslClient(mechanisms, null, “xmpp”, host, props, this);

on all locations .

Still I am getting not authorized . Do we need to enable any thing like xmpp_login ?

Am I missing some thing

not clear enough what u tried to say or supposed to do.if u r trying to connect to Facebook than just scroll upward & u will find a solution posted by me! that worked for me & others & hope will for u too!dont forget to like my solution!

Thanks what I found was it was not working for some logins .

Just now Now it is working .

Thanks

Mahesh

HI ,

I have a doubt with X-FACEBOOK.

I am confused with the login behaviour of the same .Is it possible to write applications such as chat bots with this mechanism .What is the relation to session key and chat Can a chat application using this mechanim, send message to a face book user .

regards

mahesh

Hi,

recently I try to connect Facebook Chat with smack.

I try all solutions everyone mentioned.

But I always got error message “failed using mechanism DIGEST-MD5:” and sometimes it just worked with the same code.

Can you help me to deal with this problem?

Thanks

Hi,

I am getting following error

java.lang.NullPointerException

at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:117)

at MySASLDigestMD5Mechanism.authenticate(MySASLDigestMD5Mechanism.java:43)

at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java: 308)

at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)

at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)

my code is

SASLAuthentication.registerSASLMechanism(“DIGEST-MD5”,

MySASLDigestMD5Mechanism. class);

ConnectionConfiguration config = new ConnectionConfiguration(“chat.facebook.com”,5222);

config.setSASLAuthenticationEnabled(true);

config.setRosterLoadedAtLogin (true);

XMPPConnection connection = new XMPPConnection(config);

connection.connect();

System.out.println(connection.getServiceName());

connection.login(“xxxxxxxxx”, “xxxxxx”);

How to do it, Please explain, Its urgent…

SASL authentication DIGEST-MD5 failed: not-authorized:

hi, I’m trying to using your file. but : SASL authentication DIGEST-MD5 failed: not-authorized.

my code:

SASLAuthentication.registerSASLMechanism(“DIGEST-MD5”, MySASLDigestMD5Mechanism1.class);

SASLAuthentication.supportSASLMechanism(“DIGEST-MD5”, 0);

config = new ConnectionConfiguration(“chat.facebook.com”, 5222, “chat.facebook.com”);

config.setSASLAuthenticationEnabled(true);

config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);

config.setSendPresence(false);

config.setSocketFactory(proxy.getSocketFactory());

//

conn = new XMPPConnection(config);

try {

System.out.println(“connect…”);

conn.connect();

conn.addConnectionListener(this);

System.out.println(“connected…”);

conn.login(username, pd, “leolee.com”);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

thank you.

Hi there!!

I’m quite new at this. I downloaded your file and I am get an error with your las sentence:

getSASLAuthentication().send(stanza.toString());

It’s all the time saying that the method send is not applicable for the type String. What is wrong with this???

I just want to try and connect with FB and I see that many of those who have used your file now can connect properly.

Thanks in advance.

I used your code but it no longer worked so I patched it at little and am returning it to the community. It works like a charm for facebook.

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("<response xmlns=“urn:ietf:params:xml:ns:xmpp-sasl”>");

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

}

Hello, I’m just an amateur with smack api, and I’m trying hard to connect to facebook chat using x-facebook platform.

I am using the custom SASL mechanism class for facebook authentication described above. But getting errors, actually I do not know exactly how to perform this.

public class SASLXFacebookPlatformMechanism extends SASLMechanism {
    public static final String NAME = "X-FACEBOOK-PLATFORM";     private String apiKey = "";
    private String applicationSecret = "";
    private String sessionKey = "";     /**
     * Constructor.
     */
    public SASLXFacebookPlatformMechanism1(SASLAuthentication saslAuthentication) {
        super(saslAuthentication);
    }     @Override
    protected void authenticate() throws IOException, XMPPException {
        StringBuilder stanza = new StringBuilder();
        stanza.append("<auth mechanism=\"").append(getName());
        stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
        stanza.append("</auth>");         // Send the authentication to the server
        getSASLAuthentication().send(stanza.toString());
    }     @Override
    public void authenticate(String apiKeyAndSessionKey, String host, String applicationSecret) throws IOException, XMPPException {
        if (apiKeyAndSessionKey == null || applicationSecret == null)
            throw new IllegalArgumentException("Invalid parameters");         String[] keyArray = apiKeyAndSessionKey.split("\\|", 2);
        if (keyArray.length < 2)
            throw new IllegalArgumentException("API key or session key is not present");         this.apiKey = keyArray[0];
        this.applicationSecret = applicationSecret;
        this.sessionKey = keyArray[1];         this.authenticationId = sessionKey;
        this.password = applicationSecret;
        this.hostname = host;         String[] mechanisms = { "DIGEST-MD5" };
        Map<String, String> props = new HashMap<String, String>();
        this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
        authenticate();
    }     @Override
    public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException {
        String[] mechanisms = { "DIGEST-MD5" };
        Map<String, String> props = new HashMap<String, String>();
        this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
        authenticate();
    }     @Override
    protected String getName() {
        return NAME;
    }     @Override
    public void challengeReceived(String challenge) throws IOException {
        StringBuilder stanza = new StringBuilder();
        byte[] response = null;         if (challenge != null) {
            String decodedChallenge = new String(Base64.decode(challenge));
            Map<String, String> parameters = getQueryMap(decodedChallenge);             String version = "1.0";
            String nonce = parameters.get("nonce");
            String method = parameters.get("method");             long callId = new GregorianCalendar().getTimeInMillis() / 1000L;             String sig = "api_key=" + apiKey
                            + "call_id=" + callId
                            + "method=" + method
                            + "nonce=" + nonce
                            + "session_key=" + sessionKey
                            + "v=" + version
                            + applicationSecret;             try {
                sig = md5(sig);
            }
            catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException(e);
            }             String composedResponse = "api_key=" + URLEncoder.encode(apiKey, "utf-8")
                                        + "&call_id=" + callId
                                        + "&method=" + URLEncoder.encode(method, "utf-8")
                                        + "&nonce=" + URLEncoder.encode(nonce, "utf-8")
                                        + "&session_key=" + URLEncoder.encode(sessionKey, "utf-8")
                                        + "&v=" + URLEncoder.encode(version, "utf-8")
                                        + "&sig=" + URLEncoder.encode(sig, "utf-8");             response = composedResponse.getBytes("utf-8");
        }         String authenticationText = "";         if (response != null)
            authenticationText = Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);         stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
        stanza.append(authenticationText);
        stanza.append("</response>");         // Send the authentication to the server
        getSASLAuthentication().send(stanza.toString());
    }     private Map<String, String> getQueryMap(String query) {
        Map<String, String> map = new HashMap<String, String>();
        String[] params = query.split("\\&");         for (String param : params) {
            String[] fields = param.split("=", 2);
            map.put(fields[0], (fields.length > 1 ? fields[1] : null));
        }         return map;
    }     private String md5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes("utf-8"), 0, text.length());
        return convertToHex(md.digest());
    }     private String convertToHex(byte[] data) {
        StringBuilder buf = new StringBuilder();
        int len = data.length;         for (int i = 0; i < len; i++) {
            int halfByte = (data[i] >>> 4) & 0xF;
            int twoHalfs = 0;             do {
                if (0 <= halfByte && halfByte <= 9)
                    buf.append((char) ('0' + halfByte));
                else
                    buf.append((char) ('a' + halfByte - 10));
                halfByte = data[i] & 0xF;
            }
            while (twoHalfs++ < 1);
        }         return buf.toString();
    }
}

So now I have this class, but I don’t know where I am doing wrong, may be I’m calling it in wrong manner, here is how I made use of this class.

ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(true); SASLAuthentication.registerSASLMechanism(SASLXFacebookPlatformMechanism.NAME, SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism(SASLXFacebookPlatformMechanism.NAME, 0); XMPPConnection connection = new XMPPConnection(config);
try {
    connection.connect();
} catch (XMPPException ex) {
    Logger.getLogger(FB.class.getName()).log(Level.SEVERE, null, ex);
}
SASLXFacebookPlatformMechanism1 mech=new SASLXFacebookPlatformMechanism1(connection.getSASLAuthentication());
try {
    mech.authenticate();
} catch (IOException ex) {
    Logger.getLogger(FB.class.getName()).log(Level.SEVERE, null, ex);
} catch (XMPPException ex) {
    Logger.getLogger(FB.class.getName()).log(Level.SEVERE, null, ex);
}

When I execute this code, I get a NullPointerException

java.lang.NullPointerException
    at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:514)
    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:296)
    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)

Thanks a lot Jerry. You are a saviour.

Look here’s a complete code snippet with the MD5 class.Hope this helps: http://fbserver.99k.org/Facebook.rar