【问题标题】:I want to send message to facebook friends to their message box for android我想向 facebook 朋友发送消息到他们的 android 消息框
【发布时间】:2011-12-19 05:39:55
【问题描述】:

我正在开发具有 facebook 消息发送功能的应用程序。 通过 facebook 登录,我想向我的朋友发送消息而不是墙帖,我希望发送消息而不是墙帖或其他任何东西。据我所知,有一种 XMPP 使用,请提供一些解决方案。

请提出解决方案,很紧急。

谢谢 戈帕尔

【问题讨论】:

    标签: android facebook xmpp message


    【解决方案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();
            }  
        }
    

    完成登录后。使用此链接获取名册并发送消息。

    http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/

    【讨论】:

    • 感谢分享代码!我一直在努力让它工作一段时间,你愿意分享你的项目文件,这样我就可以看看一个使用 xmpp 的工作 Facebook 消息传递应用程序。甚至只是与 facebook 消息相关联的其他文件。我在stackoverflow.com/questions/13079632/… 有一个悬而未决的问题,如果你能帮助我,我会非常乐意奖励你,谢谢!
    • 我去看看,现在试一试,我会告诉你的
    • @JanshairKhan 我对你的代码和新的 3.3.0 版本的 Smack 有疑问。您是否采用了 Smack 更新代码?
    • 请分享错误。好久没研究了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多