X-Facebook-Platform Authentication

I’ve seen several people looking for code to connect to Facebook Chat via Smack.

I’ve posted my solution to this here: http://community.igniterealtime.org/message/220711#220711 But I’m repeating in its own thread, that is not about MD5-Digest, to make it easier to find.

The attached source is a new version of the SASL Mechanism posted in the original thread.

To use it, you need an access token that you can get from Facebook (see http://developers.facebook.com/docs/authentication/). Facebook (in https://developers.facebook.com/docs/chat/) recommend you use the client flow to get the token, and that’s what I did; the server flow is supposed to yield a similar token, so it should work just as well.

The usage code looks like:

SASLAuthentication.registerSASLMechanism(SASLXFacebookPlatformMechanism.NAME, SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism(SASLXFacebookPlatformMechanism.NAME, 0);

ConnectionConfiguration config = **new** ConnectionConfiguration("chat.facebook.com", 5222);

XMPPConnection connection = **new** XMPPConnection(config);
connection.connect();
connection.login(apiKey, accessToken, "Client Name"); // Client name optional

Hope this helps,

Shai.


SASLXFacebookPlatformMechanism.java.zip (1576 Bytes)

I went to www.facebook.com/developers and created an app, which gave me an App ID and App Secret.

connection.login(apiKey, accessToken, "Client Name");

I assume apiKey is the App ID. I tried using the App Secret as the accessToken but that didn’t work. How do I get the accessToken?

Also, while we’re at it, just to make sure, please confirm: the expected behavior is at connection.login, the Facebook login screen appears, the user logs in, the user is returned to my application, and connection is now authenticated – i.e. I don’t need to worry about returning to my application or handling return messages.

Facebook is evil, whimsy and unpredictable. They’ve changed their site and moved things around.

At the time I write this, the process I had referred to earlier is described in http://developers.facebook.com/docs/authentication/client-side/#no-jssdk.

Hope this helps,

Shai.

Hello,

I am developing a simple chat client for FB. I have progressed to a state where now i have the access_token of the user who had logged in to my chat with “xmpp_login” and “offline_access” scope. Am able to successfully connect to chat.facebook.com and also login using api_key and the access_token. There is no problem in the SASL Authentication.

Now when i try to send a message using, chat.sendMessage(String) or chat.sendMessage(Message), the smack debugger shows the message has been sent. But on my facebook website, I dont see that message sent to that person. The other person is also not receiving the message. If i am able to log in there is nothing wrong with the access token right?

hellokVfox0

This is the output from Smack debugger Sent column. Is there anything wrong with the from address? or with this message structure? Am stuck at this point because i dont know how to debug this issue.

Any advice, suggestions or solutions are most welcome.

FWIW, I think it is actually your “to” address that is problematic; you should get the user-id and use that instead of the user-name. At least that’s what worked for me.

In my case, the other user visited a web page I control – so I was able to use FB’s Javascript API to get their id.

Good luck,

Shai.

Hi Karthik,

Will it be possible for you to share your client code? I am trying to develop a sample FB chat integration with my website…

Thanks,

Rohit.

Could this be committed in the repository?

Hi Shai,

When I am compiling the SASLXFacebookPlatformMechanism from your package, I am getting the following error “The method send(Packet) in the type SASLAuthentication is not applicable for the arguments (String)” for getSASLAuthentication().send(stanza.toString());

Any ideas why?

I have the smack.jar and smackx.jar in my build path.

Thanks!

I don’t know.

Perhaps it’s a change in Smack – I worked with version 3.1.0, and there SASLAuthentication had a send(String) method (at the time I write this, the latest version seems to be 3.2.2). I am currently not actively involved in the project which brought me here before, so I cannot follow and fix stuff.

Good luck,

Shai.

On second thought,

I just remembered having some problems related to this with different JDKs. Try compiling with both Java 1.6 and Java 1.7 (or, if you can get it, even Oracle Java), I think one of them may work where the other doesn’t.