Facebook asmack xmpp client returns random numbers

2019-04-02 06:51发布

I followed the guidlines given in the following SO question to connect to facebook chat using xmpp and I am able to connect to facebook and pull the correct number of contacts but when it prints the contacts they are all random numbers @chat.facebook.com and all return offline.

Android Facebook chat example project

public void connectToFb() throws XMPPException {

        ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
        config.setSASLAuthenticationEnabled(true);
        config.setSecurityMode(SecurityMode.required);
        config.setRosterLoadedAtLogin(true);
        config.setTruststorePath("/system/etc/security/cacerts.bks");
        config.setTruststorePassword("changeit");
        config.setTruststoreType("bks");
        config.setSendPresence(false);
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom());
            config.setCustomSSLContext(sc);
        } catch (GeneralSecurityException e) {
            Log.w("TAG", "Unable to use MemorizingTrustManager", e);
        }
        XMPPConnection xmpp = new XMPPConnection(config);
        try {
            xmpp.connect();
            xmpp.login("user.name", "password"); // Here you have to used only facebookusername from facebookusername@chat.facebook.com
            Roster roster = xmpp.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            System.out.println("Connected!");
            System.out.println("\n\n" + entries.size() + " buddy(ies):");
            // shows first time onliners---->
            String temp[] = new String[50];
            int i = 0;
            for (RosterEntry entry : entries) {
                String user = entry.getUser();
                Log.i("TAG", user);
            }
        } catch (XMPPException e) {
            xmpp.disconnect();
            e.printStackTrace();
        }
        }

3条回答
【Aperson】
2楼-- · 2019-04-02 07:17

Its a bug in XMPP library . There is a work around for that .

Step 1: Connect to XMPP.

Step 2: Login to facebook account through xmpp.

Step 3: get online friend list using this fql query .

    SELECT uid, name, online_presence ,
      sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Then i concat address with string uid@chat.facebook.com and communicate via XMPP.

查看更多
叼着烟拽天下
3楼-- · 2019-04-02 07:30

It sounds like you just want to readable name, so try using

rosterEntry.getName()

which returns the users name, instead of

rosterEntry.getUser()

which returns the JID.

Not sure about your offline problem though. How are you checking? You have to set up a roster listener to get changes in presence.

查看更多
太酷不给撩
4楼-- · 2019-04-02 07:35

(It's not 100% clear what problem you're having regarding the online/offline thing may be a bug or something you're doing incorrectly), but you won't get back the user's actual user IDs in the response, this is mentioned in the docs:

The user's own Jabber ID (JID) is different from the Jabber ID that their contacts will see because the translation is done internally.

查看更多
登录 后发表回答