첫째는 SASLXFacebookPlatformMechanism 클래스를 편집합니다. 이 코드를 복사하여 붙여 넣으십시오.
package com.facebook.android;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
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;
import android.util.Log;
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");
String composedResponse =
"method=" + URLEncoder.encode(method, "utf-8") +
"&nonce=" + URLEncoder.encode(nonce, "utf-8") +
"&access_token=" + URLEncoder.encode(accessToken, "utf-8") +
"&api_key=" + URLEncoder.encode(apiKey, "utf-8") +
"&call_id=0" +
"&v=" + URLEncoder.encode(version, "utf-8");
response = composedResponse.getBytes();
}
String authenticationText = "";
if (response != null) {
authenticationText = Base64.encodeBytes(response);
}
// 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;
}
}
그런 다음 당신이 로그와 함께 완료되면
private void LoginToFaceBook(){
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
xmpp = new XMPPConnection(config);
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
Log.i("XMPPClient",
"Access token to " + mFacebook.getAccessToken());
Log.i("XMPPClient",
"Access token to " + mFacebook.getAppId());
Log.i("XMPPClient",
"Access token to " + mFacebook.getAccessToken());
try {
xmpp.connect();
Log.i("XMPPClient",
"Connected to " + xmpp.getHost());
} catch (XMPPException e1) {
Log.i("XMPPClient",
"Unable to " + xmpp.getHost());
e1.printStackTrace();
}
try {
xmpp.login(PreferenceConnector.APP_ID, mFacebook.getAccessToken());
} catch (XMPPException e) {
e.printStackTrace();
}
}
페이스 북의 로그인이 방법을 사용합니다. 명단을 얻고 메시지를 보내려면이 링크를 사용하십시오.
http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/
코드를 공유해 주셔서 감사합니다! 지금 당장이 작업을 해보려고 노력하고 있습니다. 프로젝트 파일을 공유하고 싶으시겠습니까? xmpp를 사용하는 실제 facebook 메시징 앱을 살펴볼 수 있습니다. 아니면 페이스 북의 메시징과 관련된 다른 파일들. http://stackoverflow.com/questions/13079632/facebook-asmack-xmpp-client-returns-random-numbers-for-roster에서 공개 질문을 드리겠습니다. 귀하에게 보상금을 환급하는 것이 더 행복 할 것입니다. 도와 줘서 고마워! – Peter
나는 그것을 밖으로 검사하고 지금 시도하십시오, 아는 시키십시오 – Peter
@JanshairKhan 나는 당신과 가진 문제점 및 Smack의 새로운 3.3.0 버전이있다. Smack 업데이트를위한 코드를 채택 했습니까? –