SASL Authentication failed while integrating facebook chat using Smack

删除回忆录丶 提交于 2019-12-01 06:48:46

For me the solution was to not include the host part in the username when calling login() without DNS SRV and not agains the Google Talk services. This is also described in the ignite forums.

E.g.

connection.login("user@jabber.org", "password", "resource");

becomes

connection.login("user", "password", "resource");

There is a huge thread at Ignite that deals with this issue. You may want to take a look at it as there are several solutions for Java and Android given that seem to work.

I have succesfully connected using DIGEST-MD5 to facebook, the code you have posted looks good. But still we need to check the contents of your SASLDigestMD5Mechanism class

I have used the class provided in here with success

http://community.igniterealtime.org/message/200878#200878

Also you have to notice that in the DIGEST-MD5 mechanism you have to login with your facebook username and not with the email address. By default the facebook accounts don't have a username, you have to create one fisrt, you can check that in here:

http://www.facebook.com/username/

MainActivity.java

public class MainActivity extends Activity {

XMPPConnection xmpp;
ArrayList<HashMap<String, String>> friends_list;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Session.openActiveSession(this, true, new StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {

            if ( session.isOpened()){
                new testLoginTask().execute();
            }
        }
    });

}

private class testLoginTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {

        testLogin();

        return null;
    }

}

private void testLogin(){

    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
    config.setSASLAuthenticationEnabled(true);
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
    config.setDebuggerEnabled(true);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        config.setTruststoreType("AndroidCAStore");
        config.setTruststorePassword(null);
        config.setTruststorePath(null);
    } else {
        config.setTruststoreType("BKS");
        String path = System.getProperty("javax.net.ssl.trustStore");
        if (path == null)
            path = System.getProperty("java.home") + File.separator + "etc"
                                            + File.separator + "security" + File.separator
                                            + "cacerts.bks";
        config.setTruststorePath(path);
    }


    xmpp = new XMPPConnection(config);
    SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
    SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);

    try {
        xmpp.connect();
        Log.i("XMPPClient","Connected to " + xmpp.getHost());

    } catch (XMPPException e1) {
        Log.i("XMPPClient","Unable to " + xmpp.getHost());

        e1.printStackTrace();
    }
    try {


        String apiKey = Session.getActiveSession().getApplicationId();
        String sessionKey = Session.getActiveSession().getAccessToken();
        String sessionSecret = "replace with your app secret key";

        xmpp.login(apiKey + "|" + sessionKey, sessionSecret , "Application");

        Log.i("XMPPClient"," its logined ");

        Log.i("Connected",""+xmpp.isConnected());

        if ( xmpp.isConnected()){
            Presence presence = new Presence(Presence.Type.available);
            xmpp.sendPacket(presence);

        }

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

SASLXFacebookPlatformMechanism.java

public class SASLXFacebookPlatformMechanism extends SASLMechanism{

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

/**
 * Constructor.
 */
public SASLXFacebookPlatformMechanism(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 accessToken) throws IOException, XMPPException {
    if (apiKey == null || accessToken == null) {
        throw new IllegalArgumentException("Invalid parameters");
    }

    this.apiKey = apiKey;
    this.accessToken = accessToken;
    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 {
    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(accessToken, "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;
}

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!