使用XMPP Android的Facebook的聊天客户端 [英] Android-facebook chat client using xmpp

查看:403
本文介绍了使用XMPP Android的Facebook的聊天客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用asmack登录到Facebook的使用我apikey和访问令牌聊天!但是登录与以下异常失败的

Am using asmack to login to the facebook for chatting using my apikey and access token ! But the login is failing with the following exception

使用机构X-FACEBOOK平台SASL认证失败:我想从那里我可以得到一个正常工作的扩展SASLMechanism课上做的是要知道,也帮我登录目前正在使用下面的类
<一href=\"http://community.igniterealtime.org/servlet/JiveServlet/download/220711-15693/SASLXFacebookPlatformMechanism.java.zip\"相对=nofollow>自定义。和用于使登录: -

"SASL authentication failed using mechanism X-FACEBOOK-PLATFORM: " I want to know from where i can get a properly working "extends SASLMechanism" class to do it and also help me to login currently am using the following class custom . And for making the login :--

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

//但它不工作

// But it's not working

推荐答案

我测试了这一个,它为我工作。

I tested this one and it worked for me .

首先编辑SASLXFacebookPlatformMechanism类。复制并粘贴此code。

First of all Edit your SASLXFacebookPlatformMechanism class . Copy and paste this code .

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的

Then use this method in your activity class from where you want to login to 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 Android的Facebook的聊天客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆