How to Create Xmpp Chat Client for Facebook

How to create XMPP chat client for facebook?

to get access token first you have to login

fb.authorize(FacebookActivity.this, new String[] {"xmpp_login"},Facebook.FORCE_DIALOG_AUTH, new DialogListner());

SASLXFacebookPlatformMecha class

import java.io.IOException;
import java.net.URLEncoder;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.sasl.Sasl;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.Base64;

public class SASLXFacebookPlatformMecha extends SASLMechanism {

private static final String NAME = "X-FACEBOOK-PLATFORM";

private String apiKey = "";
private String access_token = "";

/**
* Constructor.
*/
public SASLXFacebookPlatformMecha(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}

@Override
protected void authenticate() throws IOException, XMPPException {

getSASLAuthentication().send(new AuthMechanism(NAME, ""));
}

@Override
public void authenticate(String apiKey, String host, String acces_token)
throws IOException, XMPPException {
if (apiKey == null || acces_token == null) {
throw new IllegalArgumentException("Invalid parameters");
}

this.access_token = acces_token;
this.apiKey = apiKey;
this.hostname = host;

String[] mechanisms = { NAME };
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 = { NAME };
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 {
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();

String composedResponse = "api_key="
+ URLEncoder.encode(apiKey, "utf-8") + "&call_id=" + callId
+ "&method=" + URLEncoder.encode(method, "utf-8")
+ "&nonce=" + URLEncoder.encode(nonce, "utf-8")
+ "&access_token="
+ URLEncoder.encode(access_token, "utf-8") + "&v="
+ URLEncoder.encode(version, "utf-8");

response = composedResponse.getBytes("utf-8");
}

String authenticationText = "";

if (response != null) {
authenticationText = Base64.encodeBytes(response,
Base64.DONT_BREAK_LINES);
}

// Send the authentication to the server
getSASLAuthentication().send(new Response(authenticationText));
}

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

I created ChatManager class

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smackx.pubsub.PresenceState;

public class FacebookChatManager {

private static FacebookChatManager chatManager;
private XMPPConnection connection;
private final String SERVER = "chat.facebook.com";
private final int PORT = 5222;
private final String FACEBOOK_MECHANISM = "X-FACEBOOK-PLATFORM";
private RosterListener rosterListner;

private FacebookChatManager(RosterListener rosterListner)
{
this.rosterListner = rosterListner;
ConnectionConfiguration connFig = new ConnectionConfiguration(SERVER,
PORT);
connFig.setSASLAuthenticationEnabled(true);
connection = new XMPPConnection(connFig);
//setup facebook authentication mechanism
SASLAuthentication.registerSASLMechanism(FACEBOOK_MECHANISM,
SASLXFacebookPlatformMecha.class);
SASLAuthentication.supportSASLMechanism(FACEBOOK_MECHANISM, 0);
}

public static FacebookChatManager getInstance(RosterListener rosterListner)
{
if(chatManager == null)
{
chatManager = new FacebookChatManager(rosterListner);
}
return chatManager;
}

public boolean connect()
{
try {
connection.connect();
return true;
} catch (XMPPException e) {
e.printStackTrace();
connection.disconnect();
}
return false;
}

public void disConnect()
{
connection.disconnect();
}

public boolean logIn(String apiKey, String accessToken)
{
try {
connection.login(apiKey, accessToken);
setPresenceState(Presence.Type.available, "");
connection.getRoster().addRosterListener(rosterListner);
return true;
} catch (XMPPException e) {
connection.disconnect();
e.printStackTrace();
}
return false;
}

public Roster getRoster()
{
return connection.getRoster();
}

public Chat createNewChat(String user, MessageListener messageListner)
{
return connection.getChatManager().createChat(user, messageListner);
}

public void registerNewIncomingChatListner(ChatManagerListener chatManagerListner)
{
connection.getChatManager().addChatListener(chatManagerListner);
}

public void setPresenceState(Type precenseType, String status)
{
Presence presence = new Presence(precenseType);
presence.setStatus(status);
connection.sendPacket(presence);
}

public Presence getUserPresence(String userId)
{
return connection.getRoster().getPresence(userId);
}
}

at the end to use that FacebookChatManager class note that rosterListnr is used to get info about your friends state change implement one as you want

FacebookChatManager facebookChatManager = FacebookChatManager.getInstance(rosterListner);

if (facebookChatManager.connect()) {
if (facebookChatManager.logIn(FacebookActivity.APP_ID,
access_token)) {
return facebookChatManager.getRoster();
}
}

How to REALLY create a Facebook chat client for Android?

Just a bit of information.

  • asmack is a fork of Smack designed specifically to enable Smack on the Android platform.
  • Smack is a client library for XMPP, which as you know is how you can chat with FB users.
  • SASLAuthentication is simply an authentication means used within XMPP to login to the XMPP server. In your case this server is the FB one.

There is a discussion thread at Ignite Realtime on connecting to FB with Smack (Several in fact if you do a search).

As a side note, I believe I read somewhere that asmack is no longer being maintained/developed. I think most developers are simply making a few tweaks to the Smack source to make it work on Android themselves.

Facebook xmpp chat message

Try not putting a from address on your message. The server should add that for you.

XMPP - Facebook - How can my client implementation receive messages sent by other client implementations?

If you send an IQ to enable the feature the server replies with a <feature-not-implemented /> error IQ, which as specified here means it is not supporting XEP 0280. I just checked myself through the Audium XML console.

IQ:

<iq xmlns='jabber:client'
from='my.facebook.username@chat.facebook.com/Mac-Pro-di-Michele_65563c5f_4D689E59FB8A5'
to='chat.facebook.com'
type='set'
id='enable1'>
<enable xmlns='urn:xmpp:carbons:2'/>
</iq>

Reply:

<iq xmlns='jabber:client'
from='chat.facebook.com'
to='my.facebook.username@chat.facebook.com/Mac-Pro-di-Michele_65563c5f_4D689E59FB8A5' type='error'
id='enable1'>
<enable xmlns='urn:xmpp:carbons:2'/>
<error code='501' type='cancel'>
<feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>

XMPP Android - build client to chat on facebook or facebook chat integration

Check out the asmack library: it's Smack with patches to work work on Android.

http://code.google.com/p/asmack/



Related Topics



Leave a reply



Submit