【问题标题】:Android-facebook chat client using xmpp使用 xmpp 的 Android-facebook 聊天客户端
【发布时间】:2012-05-11 09:53:38
【问题描述】:

我正在使用 asmack 登录 facebook,使用我的 apikey 和访问令牌聊天!但是登录失败,出现以下异常

“使用 X-FACEBOOK-PLATFORM 机制进行 SASL 身份验证失败:”我想知道我可以从哪里获得一个正常工作的“扩展 SASLMechanism”类来执行此操作,并帮助我当前使用以下类登录 custom 。并进行登录:--

SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",     SASLXFacebookPlatformMechanism.class);     
            SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);     
            connection.connect(); 
            connection.login(facebook.getAppId(), facebook.getAccessToken());

// 但它不起作用

【问题讨论】:

  • 哥帕拉你有这个问题的解决方案

标签: android facebook xmpp asmack


【解决方案1】:

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

首先编辑你的 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 的活动类中使用此方法

private void LoginToFaceBook(){
        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());




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

【讨论】:

  • 那么我们必须在其中添加用户名和密码
  • 您需要在此行中输入访问令牌。 xmpp.login(PreferenceConnector.APP_ID, mFacebook.getAccessToken());
  • 手动我需要输入accesstoken啊老兄,首先这是我需要登录facebook然后我必须使用这个代码啊
  • ,我获得了用于登录的 accesstoken 和 apikey,它显示这些错误 03-28 07:07:33.506: W/System.err(8440): java.security.KeyStoreException: java.security.NoSuchAlgorithmException : KeyStore jks implementation not found 这是什么,如何解决
  • 我使用了你的代码,它显示了一些类似这样的错误 NoSuchAlgorithmException: KeyStore jks implementation not found
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 2012-06-14
  • 2012-10-30
  • 2015-06-01
  • 2023-03-29
  • 2012-04-04
  • 1970-01-01
相关资源
最近更新 更多