java - 带有 Java Asmack 库和 X-FACEBOOK-PLATFORM 的 XMPP

标签 java android facebook xmpp asmack

我故意复制粘贴了 this question不要为重复而生气。 该主题中有几个我不清楚的时刻:

1)

xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application");

sessionKey is the second part of the access token. If the token is in this form, AAA|BBB|CCC, the BBB is the session key

但我的访问 token 看起来像:BAADcfjCWMLABAIyzRSZA69eAtA9Dr3EQVlXA8Ql6rr5odDWxNYZCHhssiaar8S0gaPLZAm1ZBKCqWO3QFegJPR39hT0JR5ZCyIP1AJZC19qh9mFAExUd9KDjJ05yjE3IUZ

所以我看不到任何“第二部分”...

2)

sessionSecret is obtained using the old REST API with the method auth.promoteSession. To use it, it's needed to make a Http get to this url:

https://api.facebook.com/method/auth.promoteSession?access_token=yourAccessToken

Despite of the Facebook Chat documentation says that it's needed to use your application secret key, only when I used the key that returned that REST method I was able to make it works. To make that method works, you have to disable the Disable Deprecated Auth Methods option in the Advance tab in your application settings.

我读过 here REST 已被弃用,

We will not be supporting this method in the Graph API.

我该怎么办?我只使用图形 API。有没有其他方法获取sessionSecret?

谢谢!

最佳答案

我测试了这个,它对我有用。

首先编辑您的 SASLXFacebookPlatformMechanism 类。复制并粘贴此代码。

package com.facebook.android;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.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;

import android.util.Log;


public class SASLXFacebookPlatformMechanism extends SASLMechanism {

    private 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 {
        getSASLAuthentication().send(new AuthMechanism(NAME, ""));
    }

    @Override
    public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException {
        if (apiKey == null || accessToken == null) {
            throw new IllegalArgumentException("Invalid parameters");
        }

        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
    public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException {
        String[] mechanisms = { "DIGEST-MD5" };
        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");

            String composedResponse =
                "method=" + URLEncoder.encode(method, "utf-8") +
                        "&nonce=" + URLEncoder.encode(nonce, "utf-8") +
                        "&access_token=" + URLEncoder.encode(accessToken, "utf-8") +
                        "&api_key=" + URLEncoder.encode(apiKey, "utf-8") +
                        "&call_id=0" +
                        "&v=" + URLEncoder.encode(version, "utf-8");
            response = composedResponse.getBytes();
        }

        String authenticationText = "";

        if (response != null) {
            authenticationText = Base64.encodeBytes(response);
        }

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

然后在您要登录 facebook 的 Activity 类中使用此方法。

private void testLogin(){
        ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
        config.setSASLAuthenticationEnabled(true);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
        xmpp = new XMPPConnection(config);
        SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
        SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
        Log.i("XMPPClient",
                "Access token to " + mFacebook.getAccessToken());
        Log.i("XMPPClient",
                "Access token to " + mFacebook.getAppId());
        Log.i("XMPPClient",
                "Access token to " + mFacebook.getAccessToken());
        try {
            xmpp.connect();
            Log.i("XMPPClient",
                    "Connected to " + xmpp.getHost());

        } catch (XMPPException e1) {
            Log.i("XMPPClient",
                    "Unable to " + xmpp.getHost());

            e1.printStackTrace();
        }
        try {
            xmpp.login(PreferenceConnector.APP_ID, mFacebook.getAccessToken());

            getRoster(xmpp);


        } catch (XMPPException e) {
            e.printStackTrace();
        }  
    }

关于java - 带有 Java Asmack 库和 X-FACEBOOK-PLATFORM 的 XMPP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7790527/

相关文章:

android - 在android中的位图中腾出更多空间

android - forName 找不到类

facebook - 我想在每个页面上显示一个 FB 评论框,但有自己的特定于该页面的评论

javascript - Angularjs $location.path() 不重定向

java - jetty 配置错误

android - 如何在 Android 中每 10 分钟运行一次方法?

java - 与 HibernateTemplate 和 SessionFactory 的单一数据库连接

ios - 线程 1 : SIGABRT Error when testing FB Login in XCODE

java - 将字符串String对象转换为可迭代的json对象

java - 审查输入文本文件并将其写入输出文本文件