无法使用smack API登录FB

时间:2015-02-25 03:43:52

标签: java facebook facebook-graph-api oauth-2.0 smack

我正在尝试使用smack API使用访问令牌和API密钥登录FB。这是我的代码

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);
    ....

当我运行上面的代码时,它会抛出 2015年2月25日上午9:10:34 org.jivesoftware.smack.provider.UrlProviderFileInitializer初始化 信息:加载文件的提供者[classpath:META-INF / core.providers] 2015年2月25日上午9:10:34 ** org.jivesoftware.smack.provider.UrlProviderFileInitializer初始化 信息:加载文件的提供者[classpath:META-INF / extension.providers] 线程&#34; main&#34;中的例外情况SASL身份验证X-FACEBOOK-PLATFORM失败:未经授权:     在org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:342)     在org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)     在org.jivesoftware.smack.Connection.login(Connection.java:368)     在com.fb.xmppchat.app.FBConsoleChatApp.connect(FBConsoleChatApp.java:61)     在com.fb.xmppchat.app.Test.main(Test.java:14)






非常感谢您的帮助。提前谢谢。

0 个答案:

没有答案
相关问题