使用 Smack 集成 Facebook 聊天时 SASL 身份验证失败

发布于 2024-10-18 13:49:04 字数 1081 浏览 4 评论 0原文

我正在尝试使用 smack API 集成 facebook 聊天。但是我收到一条错误消息,告诉我使用摘要 md5 身份验证失败...

这是身份验证代码:

 SASLAuthentication.registerSASLMechanism("DIGEST-MD5", SASLDigestMD5Mechanism.class);
  SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0);

    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222);
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(true);
    connection.connect();
    connection.login(userName, password);

下面是我运行它时收到的错误:

Exception in thread "main" SASL authentication failed using mechanism DIGEST-MD5: 
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:325)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
    at JabberSmackAPIFacebook.login(JabberSmackAPIFacebook.java:31)
    at JabberSmackAPIFacebook.main(JabberSmackAPIFacebook.java:77)

我可以成功连接到 gtalk,但是我没有成功 vit fb... 苏莫能告诉我问题是什么吗

I am trying to integrate facebook chat using smack API.But i get an error telling authentication failed using digest md5...

Here s the code for authentication:

 SASLAuthentication.registerSASLMechanism("DIGEST-MD5", SASLDigestMD5Mechanism.class);
  SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0);

    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222);
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(true);
    connection.connect();
    connection.login(userName, password);

below is the error i get wen i run it:

Exception in thread "main" SASL authentication failed using mechanism DIGEST-MD5: 
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:325)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
    at JabberSmackAPIFacebook.login(JabberSmackAPIFacebook.java:31)
    at JabberSmackAPIFacebook.main(JabberSmackAPIFacebook.java:77)

I can successfully connect to gtalk but am having no success vit fb...
can sumone tel me wat s the problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-10-25 13:49:04

对我来说,解决方案是在不使用 DNS SRV 且不使用 Google Talk 服务的情况下调用 login()不在用户名中包含主机部分。这也在 ignite 论坛中进行了描述。

例如

connection.login("[email protected]", "password", "resource");

变成

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

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("[email protected]", "password", "resource");

becomes

connection.login("user", "password", "resource");
真心难拥有 2024-10-25 13:49:04

Ignite 上有一个巨大的线程处理这个问题。您可能想看看它,因为有几种针对 Java 和 Android 的解决方案似乎可行。

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.

森林很绿却致人迷途 2024-10-25 13:49:04

我已经使用 DIGEST-MD5 成功连接到 facebook,您发布的代码看起来不错。
但我们仍然需要检查您的 SASLDigestMD5Mechanism 类的内容

我已成功使用此处提供的类

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

另外,您还必须注意,在 DIGEST-MD5 机制中,您必须使用您的 facebook 用户名登录,而不是使用电子邮件登录地址。默认情况下,Facebook 帐户没有用户名,您必须先创建一个用户名,您可以在此处查看:

http://www.facebook.com/用户名/

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/

め可乐爱微笑 2024-10-25 13:49:04

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文