如何使用 xmpp 连接 Facebook 聊天,我想输入朋友的用户名,然后聊天显示 SASL 身份验证失败
Posted
技术标签:
【中文标题】如何使用 xmpp 连接 Facebook 聊天,我想输入朋友的用户名,然后聊天显示 SASL 身份验证失败【英文标题】:How to connect Facebook chat using xmpp,i want to enter username of friends and then chat-showing SASL Authentication failed 【发布时间】:2014-03-26 12:01:47 【问题描述】:我可以连接xmpp for gtalk,但我不知道如何连接xmpp for facebook chat,我搜索了很多,然后我写了一些代码,它也不起作用,
现在我正在尝试这样,用户需要输入他的用户名和密码,然后用户必须输入他的朋友用户名和消息然后聊天。
XMPPClient.java
public class XMPPClient extends Activity
private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private SettingsDialog mDialog;
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection connection;
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle)
super.onCreate(icicle);
Log.i("XMPPClient", "onCreate called");
setContentView(R.layout.main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mRecipient = (EditText) this.findViewById(R.id.recipient);
Log.i("XMPPClient", "mRecipient = " + mRecipient);
mSendText = (EditText) this.findViewById(R.id.sendText);
Log.i("XMPPClient", "mSendText = " + mSendText);
mList = (ListView) this.findViewById(R.id.listMessages);
Log.i("XMPPClient", "mList = " + mList);
setListAdapter();
// Dialog for getting the xmpp settings
mDialog = new SettingsDialog(this);
// Set a listener to show the settings dialog
Button setup = (Button) this.findViewById(R.id.setup);
setup.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
mHandler.post(new Runnable()
public void run()
mDialog.show();
);
);
// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
String to = mRecipient.getText().toString();
String text = mSendText.getText().toString();
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
connection.sendPacket(msg);
messages.add(connection.getUser() + ":");
messages.add(text);
setListAdapter();
);
/**
* Called by Settings dialog when a connection is establised with the XMPP server
*
* @param connection
*/
public void setConnection
(XMPPConnection
connection)
this.connection = connection;
if (connection != null)
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener()
public void processPacket(Packet packet)
Message message = (Message) packet;
if (message.getBody() != null)
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
messages.add(fromName + ":");
messages.add(message.getBody());
// Add the incoming message to the list view
mHandler.post(new Runnable()
public void run()
setListAdapter();
);
, filter);
private void setListAdapter
()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item,
messages);
mList.setAdapter(adapter);
settings.java
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener
private XMPPClient xmppClient;
public SettingsDialog(XMPPClient xmppClient)
super(xmppClient);
this.xmppClient = xmppClient;
protected void onStart()
super.onStart();
setContentView(R.layout.settings);
getWindow().setFlags(4, 4);
setTitle("XMPP Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
public void onClick(View v)
String host = getText(R.id.host);
String port = getText(R.id.port);
String service = getText(R.id.service);
String username = getText(R.id.userid);
String password = getText(R.id.password);
//GTalk...Host name : talk.google.com The port number is 5222 service name : gmail.com
//Yahoo...Host name : iopibm.msg.yahoo.com The default port is 5061 service name : yahoo.com
//Facebook Hostname : chat.facebook.com The port number is 5222 service = chat.facebook.com for authentication SASLAuthentication.supportSASLMechanism("PLAIN", 0);
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
XMPPConnection xmpp = new XMPPConnection(config);
try
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
xmpp.connect();
xmpp.login("268651109963113", "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY", "Application");
catch (XMPPException e)
xmpp.disconnect();
e.printStackTrace();
private String getText(int id)
EditText widget = (EditText) this.findViewById(id);
return widget.getText().toString();
SASLXFacebookPlatformMechanism .java
public class SASLXFacebookPlatformMechanism extends SASLMechanism
private static final String NAME = "X-FACEBOOK-PLATFORM";
private String apiKey = "268651109963113";
private String access_token = "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY";
/**
* 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 acces_token)
throws IOException, XMPPException
if (apiKey == null || acces_token == null)
throw new IllegalArgumentException("Invalid parameters");
this.access_token = acces_token;
this.apiKey = apiKey;
this.hostname = host;
String[] mechanisms = NAME ;
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 = NAME ;
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(access_token, "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;
现在我更新的代码在这些行中抛出 NPE
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
connection.sendPacket(msg);
首先它会从那里打开对话框,我们需要输入用户名和密码,然后想输入朋友列表的用户名,然后是味精,然后像这样点击发送,我正在尝试,,,我正在输入用户名称也正确,但单击发送按钮时显示 NUll ponter 异常
【问题讨论】:
尝试清理您的项目,如果清理项目不起作用,则只需重新启动一次 Eclipse 并检查。 是的,我尝试了所有方法 @InnocentKiller,你能检查我的代码是正确还是错误 好的,那么问题可能是您在某处有 2 个 jar 文件,其中包含相同的包和类。 你的项目中有android_maps_lib
吗。
不,伙计,我有 android_support4jar,asmack-2010.04.02.jar,asmack-2010.05.07.jar,asmack-issue15.jar,asmack-jse-buddycloud-2010.12.11.jar,httpclient -4.1.3.jar,httpcore-4.1.4.jar,org.xbill.dns_2.1.6.jar,smack.jar,smackx.jar,smackx-debug.jar,smackx-jingle.jar,xpp3-1.1.4c .jar,我有这么多,如果我删除一个它会在代码中的某处显示错误@InnocentKiller
【参考方案1】:
我认为问题可能是您在某处有 2 个 jar 文件,其中包含相同的包和类。因此,我强烈建议您再次检查所有 jar 文件并删除导致问题的任何人。
从其中一个 JAR 文件中删除此包将解决问题。
【讨论】:
Dalvik format failed with error 1 这个问题解决了,感谢您的回答,但是如何使用 xmpp 连接 facebook 聊天 @KarthickM,我对此一无所知,真的很抱歉。【参考方案2】:有一个官方Facebook Chat API documentation,您在实现聊天客户端时需要遵循它 - 要对用户进行身份验证,您需要向他展示 WebView 表单,他在 Facebook 上对您的应用程序进行身份验证,获取身份验证令牌,然后 strong> 令牌将用于 X-FACEBOOK-PLATFORM SASL 机制。
【讨论】:
autentical token 你是在说 apikey 和 accesstoken 请阅读完整的身份验证流程说明:api 密钥和访问令牌是您在连接到 facebook 时提供的,而身份验证令牌是 facebook 在允许您的应用访问他的 Facebook 帐户时给用户的。 ,你有没有关于如何连接facebook xmpp聊天的示例或完整代码 为了登录,我们必须像这样 example@gmail.com 或 example@chatfacebook.com以上是关于如何使用 xmpp 连接 Facebook 聊天,我想输入朋友的用户名,然后聊天显示 SASL 身份验证失败的主要内容,如果未能解决你的问题,请参考以下文章
适用于 Android 的 XMPP/facebook 聊天连接
如何使用 XMPP 和 BOSH 实现 Facebook 聊天
在android中使用xmpp的android-facebook聊天客户端