Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:21:46+00:00 2026-06-06T13:21:46+00:00

I’m creating XMPP client for FACEBOOK . i did this for gmail, now i

  • 0

I’m creating XMPP client for FACEBOOK. i did this for gmail, now i have to create same for FaceBook. i googled a lot for this got some code, still i’m getting this type of errors Not connected to server and service-unavailable(503)

here i’m sharing the code what i did.

public class ClientJabberActivity extends Activity {

ArrayList<String> m_discussionThread;
ArrayAdapter<String> m_discussionThreadAdapter;
XMPPConnection m_connection;
private Handler m_handler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    m_handler = new Handler();

    try {
        initConnection();
    } catch (XMPPException e) {
        e.printStackTrace();
    }

    final EditText recipient = (EditText) this.findViewById(R.id.recipient);
    final EditText message = (EditText) this.findViewById(R.id.message);
    ListView list = (ListView) this.findViewById(R.id.thread);

    m_discussionThread = new ArrayList<String>();
    m_discussionThreadAdapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item, m_discussionThread);
    list.setAdapter(m_discussionThreadAdapter);

    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String to = recipient.getText().toString();
            String text = message.getText().toString();

            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            m_connection.sendPacket(msg);
            m_discussionThread.add(" Me  : ");
            m_discussionThread.add(text);
            m_discussionThreadAdapter.notifyDataSetChanged();
        }
    });
}

private void initConnection() throws XMPPException {

    ConnectionConfiguration config = new ConnectionConfiguration(
            "chat.facebook.com", 5222, "chat.facebook.com");
    config.setSASLAuthenticationEnabled(true);
    m_connection = new XMPPConnection(config);
    try {
        SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",
                SASLXFacebookPlatformMechanism.class);
        SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
        m_connection.connect();          
        m_connection.login(apiKey + "|" + sessionKey, sessionSecret, "Application");
    } catch (XMPPException e) {
        m_connection.disconnect();
        e.printStackTrace();
    }

    Presence presence = new Presence(Presence.Type.available);
    m_connection.sendPacket(presence);

    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);

    m_connection.addPacketListener(new PacketListener() {
        public void processPacket(Packet packet) {
            Message message = (Message) packet;
            if (message.getBody() != null) {
                String fromName = StringUtils.parseBareAddress(message
                        .getFrom());
                m_discussionThread.add(fromName + ":");
                m_discussionThread.add(message.getBody());

                m_handler.post(new Runnable() {
                    public void run() {
                        m_discussionThreadAdapter.notifyDataSetChanged();
                    }
                });
            }
        }
    }, filter);

    ChatManager chatmanager = m_connection.getChatManager();
    chatmanager.addChatListener(new ChatManagerListener() {
        public void chatCreated(final Chat chat,
                final boolean createdLocally) {
            chat.addMessageListener(new MessageListener() {
                public void processMessage(Chat chat, Message message) {
                    System.out.println("Received message: "
                            + (message != null ? message.getBody() : "NULL"));
                    Log.i("CHAT USER",
                            "Received message is: " + message.getBody());
                }
            });
        }
    });
}
 }

and this class SASLXFacebookPlatformMechanism

How can i login like this xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application"); i know how to get acessToken, Application Key for facebook. i don’t know about sessionKey, sessionSecret how to get those values and how to solve this problem.

If i use xmpp.login(apiKey, accessToken, "Application"); i am getting this error –IllegalArgumentException: API key or session key is not present

EDIT: Finally i got solution from Amal solution : xmpp.login(apiKey, accessToken, "Application");

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:21:48+00:00Added an answer on June 6, 2026 at 1:21 pm

    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();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.