使用OAuth 2.0与SMACK Java库进行X-FACEBOOK-PLATFORM身份验证

als*_*scu 9 xmpp smack oauth-2.0 facebook-chat x-facebook-platform

请先在这里发帖,请保持温柔.我正在使用Smack库构建一个Facebook聊天客户端.我正在使用X-FACEBOOK-PLATFORM方法,以免保存任何密码.我使用oauth 1.0正常工作,并希望将其更改为2.0,导致10月1日截止日期; p.根据我的理解,为了迁移到2.0,我必须做的唯一事情是删除"sig"和"session_key"参数并添加"access_token"参数,但我得到的是"SASL身份验证X-FACEBOOK- PLATFORM失败:未经授权:".我正在使用这个自定义SASLMechanism类:

package smackresearching;
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;
    }
    }
Run Code Online (Sandbox Code Playgroud)

这是主要代码:

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


try {
    //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 = "3290282390339";
    String accessToken = "ADSJGFGFKDKSJKSJD0-43DKJSDJKSDKJSD094JJSDKJSDKJDSNDSKJEWEWNSDLkljdkjs";
    connection.login(apiKey, accessToken);
...
Run Code Online (Sandbox Code Playgroud)

提前感谢任何建议.

Ale*_*ode 5

composResponse字符串中的access_token参数缺少&符号.这是拼写错误吗?

你可以发布你要发送的authenticationText吗?