mardi 24 février 2015

Unable to login into FB using smack API

I am trying to login into FB using access token and API key using smack API. Here is my 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 {
System.out.println("autneticate");
this.apiKey = apiKey;
this.accessToken = accessToken;
this.hostname = host;

if (apiKey == null || accessToken == null) {
throw new IllegalArgumentException("Invalid parameters");
}

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;
}
}



public class FBConsoleChatApp {

public static final String FB_XMPP_HOST = "chat.facebook.com";
public static final int FB_XMPP_PORT = 5222;

private ConnectionConfiguration config;
private XMPPConnection connection;
private BidiMap friends = new DualHashBidiMap();
private FBMessageListener fbml;

public XMPPConnection connect() throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
//config.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPConnection(config);

//ESTA LINEA HACE QUE NO DE TIMEOUT
SmackConfiguration.setPacketReplyTimeout(15000);
XMPPConnection.DEBUG_ENABLED = true;
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
connection.connect();
String apiKey = "145634995501456";
String accessToken = "CAACEdEose0cBABHNPVMaDSwaDO7c9iiDd6nJ5cj3x1Aqr64hNYST2yIsZAXwtS6jOImU20KIwO4";
connection.login(apiKey, accessToken);
....


When I run above code it throws Feb 25, 2015 9:10:34 AM org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize INFO: Loading providers for file [classpath:META-INF/core.providers] Feb 25, 2015 9:10:34 AM** org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize INFO: Loading providers for file [classpath:META-INF/extension.providers] Exception in thread "main" 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:368) at com.fb.xmppchat.app.FBConsoleChatApp.connect(FBConsoleChatApp.java:61) at com.fb.xmppchat.app.Test.main(Test.java:14)












Your help is highly appreciate. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire