Hi,
First off, sorry to bring up this question about this error message:
SASL authentication X-FACEBOOK-PLATFORM failed: not-authorized
I am building a simple facebook chat client using JAVA, smack and X-FACEBOOK-PLATFORM for authentication for school project.
I have a valid access token through the facebook-documented client side flow. I have the xmpp_permissions obtained.
I found the custom X-FACEBOOK-PLATFORM elsewhere on this forums and stackoverflow. Trying to reuse it, but getting the above said error.
Here is the class and the code.
import java.io.IOException;
import java.net.URLEncoder;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map; import 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; 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;
}
}
Here is the main code:
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; public class try2
{
public static String API_KEY="1516721708552198"; // my app key
public static String ACTOKEN=""; //valid token with xmpp permissions public static void main(String[] args)
{
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPConnection(config); SmackConfiguration.setPacketReplyTimeout(15000);
XMPPConnection.DEBUG_ENABLED = true;
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class); SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
try
{
connection.connect();
} catch (XMPPException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} try
{
connection.login(API_KEY,ACTOKEN);
} catch (XMPPException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
Stack Trace:
SASL authentication X-FACEBOOK-PLATFORM failed: not-authorized:
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java: 342)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)
at org.jivesoftware.smack.Connection.login(Connection.java:366)
at try2.main(try2.java:37)
I searched, but found mostly old post from 2012 and also with no working solutions for me. Whatever minor changes were suggested I tried them. But to no use.
Any kind of help is appreciated. Also, very much a java and smack newbie here. So even the basic errors I might have done, please do point out and guide me. Thanks in advance.
Message was edited by: Dhruv Sangvikar to add syntax highlighting