如何在 Android (2014) 中使用 aSmack XMPP 库实现 facebook 聊天? [关闭]
Posted
技术标签:
【中文标题】如何在 Android (2014) 中使用 aSmack XMPP 库实现 facebook 聊天? [关闭]【英文标题】:How do I implement facebook chat using aSmack XMPP library in Android (2014)? [closed] 【发布时间】:2014-01-14 13:53:21 【问题描述】:我搜索了很多地方以找到将 facebook 聊天集成到 android/Smack 中的方法,但没有一个完全有效。有人可以提供与最新版本的 API 一起使用的工作示例吗?
【问题讨论】:
【参考方案1】:使用 X-FACEBOOK-PLATFORM SASLAuthentication 进行身份验证。已在 2014 年 1 月 14 日验证使用 Android 4.2.2。
jabber ID 不是 username@chat.facebook.com。它被解析为一个数字 id,您可以对照花名册进行检查。
ChatActivity.java
public void connectToFb() throws XMPPException
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(SecurityMode.required);
config.setSendPresence(false);
XMPPConnection xmpp = new XMPPConnection(config);
try
xmpp.connect();
xmpp.login(Session.getActiveSession().getApplicationId(), Session.getActiveSession().getAccessToken(), "Application");
//send a chat message
ChatManager chatmanager = xmpp.getChatManager();
Chat newChat = chatmanager.createChat("<jabber-id-here>@chat.facebook.com", new MessageListener()
@Override
public void processMessage(Chat chat, Message msg)
Log.d("test", "message sent = "+ msg);
);
newChat.sendMessage("Cowdy!");
//get roster
Roster roster = xmpp.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("Connected!");
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry entry : entries)
Log.i("test", entry.getName());
Log.i("test", entry.getUser());
catch (XMPPException e)
xmpp.disconnect();
e.printStackTrace();
SASLXFacebookPlatformMechanism.java
public class SASLXFacebookPlatformMechanism extends SASLMechanism
public 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
// Send the authentication to the server
getSASLAuthentication().send(new AuthMechanism(getName(), ""));
@Override
public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException
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
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() / 1000L;
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;
使用 SASL/Plain 的身份验证流程(当提供用户名和密码时)
参考Android Facebook chat example project
【讨论】:
以上是关于如何在 Android (2014) 中使用 aSmack XMPP 库实现 facebook 聊天? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
我可以在我的 AS3 Air 中使用 Z-Push 来玩 Android 游戏吗
AS3:如何在外部 swf 上的文本字段中使用 Android 默认字体?
如何使用Android Studio开发/调试Android源码