Facebook 聊天使用 xmpp 问题-NoSuchAlgorithmException: KeyStore jks implementation not found

Posted

技术标签:

【中文标题】Facebook 聊天使用 xmpp 问题-NoSuchAlgorithmException: KeyStore jks implementation not found【英文标题】:Facebook chat using xmpp problems-NoSuchAlgorithmException: KeyStore jks implementation not found 【发布时间】:2014-04-01 10:23:08 【问题描述】:

我想使用 xmpp 显示 facebook 聊天,我想列出在线朋友,然后使用 xmpp 聊天。因为这会出现很多错误;现在我正在输入这个代码,这是正确的代码,然后我点击了这个链接对于 ssl 商店 http://randomizedsort.blogspot.in/2010/09/step-to-step-guide-to-programming.html 和这个 Using the android TrustStore for aSmack in Android 4+ (ICS) 也是。

Button setup = (Button) this.findViewById(R.id.setup);
        setup.setOnClickListener(new View.OnClickListener() 
            public void onClick(View view) 
                mHandler.post(new Runnable() 
                    public void run()
                    
                       // mDialog.show();
                       loginfacebook();


                    
                );
            
        );

public void loginfacebook()
    
        Session currentSession = Session.getActiveSession();
        if (currentSession == null || currentSession.getState().isClosed()) 

         System.out.println("your current fb session is closed........ or you are not logged in...............");
            Session session = new Session.Builder(XMPPClient.this).build();
            Session.setActiveSession(session);
            currentSession = session;
        

        if (currentSession.isOpened()) 
            // Do whatever u want. User has logged in
         System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr...............");
          Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() 
         @SuppressWarnings("deprecation")
        @Override
             public void call(final Session session, SessionState state, Exception exception) 
               if (session.isOpened()) 
               ////do after login work here....................
                 //  mDialog.show();

                   System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken());
                      ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
                        config.setSASLAuthenticationEnabled(true);
                        config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                        XMPPConnection xmpp = new XMPPConnection(config);
                        SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
                        SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);

                        try 
                            xmpp.connect();


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

                            e1.printStackTrace();
                        
                        try 
                            xmpp.login(fb.getAppId(), session.getAccessToken());



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

        else if (!currentSession.isOpened()) 
            // Ask for username and password
         System.out.println("your are going to ask for authenticationnnnnnnnnnnnn...............");
            OpenRequest op = new Session.OpenRequest((Activity) XMPPClient.this);

            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
           // permissions.add("publish_stream");
            //permissions.add("user_likes");
            permissions.add("email");
            permissions.add("user_birthday");
            permissions.add("friends_birthday");
            op.setPermissions(permissions);

            Session session = new Session.Builder(XMPPClient.this).build();
            Session.setActiveSession(session);
            session.openForPublish(op);
        
    

    public void call(Session session, SessionState state, Exception exception) 
    
         



      public void onActivityResult(int requestCode, int resultCode, Intent data) 
     System.out.println("Face boookkk has returned valuessssssssssssssssssssssssss...............");
          super.onActivityResult(requestCode, resultCode, data);
          if (Session.getActiveSession() != null)
          Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

          Session currentSession = Session.getActiveSession();

          if (currentSession == null || currentSession.getState().isClosed()) 
           System.out.println("unable to create sessionnnnnnnnnnnn...............");
              Session session = new Session.Builder(XMPPClient.this).build();
              Session.setActiveSession(session);
              currentSession = session;
          

          if (currentSession.isOpened()) 
           System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr...............");
           Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() 
         @SuppressWarnings("deprecation")
        @Override
             public void call(final Session session, SessionState state, Exception exception) 
               if (session.isOpened()) 
               // after login workkkkkkkkkkk
                   System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken());
            /*     ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
                config.setSASLAuthenticationEnabled(true);
                config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                XMPPConnection xmpp = new XMPPConnection(config);
                SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
                SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);*/


             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
              ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
                config.setSASLAuthenticationEnabled(true);
                config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                XMPPConnection xmpp = new XMPPConnection(config);
                SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
                SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
               config.setTruststoreType("AndroidCAStore");
              config.setTruststorePassword(null);
             config.setTruststorePath(null);
            try 
                xmpp.connect();


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

                e1.printStackTrace();
            
            try 
                xmpp.login(fb.getAppId(), session.getAccessToken());



             catch (XMPPException e) 
                e.printStackTrace();
             
           else 
              ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
                config.setSASLAuthenticationEnabled(true);
                config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                XMPPConnection xmpp = new XMPPConnection(config);
                SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
                SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
               config.setTruststoreType("BKS");
              String path = System.getProperty("javax.net.ssl.trustStore");
              if (path == null)
                  path = System.getProperty("java.home") + File.separator + "etc"
                      + File.separator + "security" + File.separator
                      + "cacerts.bks";
           config.setTruststorePath(path);
          try 
                xmpp.connect();


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

                e1.printStackTrace();
            
            try 
                xmpp.login(fb.getAppId(), session.getAccessToken());



             catch (XMPPException e) 
                e.printStackTrace();
             
          


               
         
          );
      
     I am searching lot for connecting facebook xmpp not getting proper solutions.

【问题讨论】:

【参考方案1】:

对于那个错误,我使用了这样的代码,在搜索了很多链接后,我得到了一个很好的链接,它解决了我当前的问题Successfull authentication but status is always "offline" and rooster is empty (Android,XMPP,Facebook)

我是这样使用我的代码的

 if (currentSession.isOpened()) 
            // Do whatever u want. User has logged in
         System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr...............");
          Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() 
         @SuppressWarnings("deprecation")
        @Override
             public void call(final Session session, SessionState state, Exception exception) 
               if (session.isOpened()) 
               ////do after login work here....................

                   System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken());

                        ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
                        config.setDebuggerEnabled(true);
                        config.setSASLAuthenticationEnabled(true);
                       config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
                           config.setTruststoreType("AndroidCAStore");
                           config.setTruststorePassword(null);
                           config.setTruststorePath(null);
                        else 
                           config.setTruststoreType("BKS");
                           String path = System.getProperty("javax.net.ssl.trustStore");
                           if (path == null)
                               path = System.getProperty("java.home") + File.separator + "etc"
                                   + File.separator + "security" + File.separator
                                   + "cacerts.bks";
                           config.setTruststorePath(path);
                       
                      XMPPConnection  xmpp = new XMPPConnection(config);
                        SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
                        SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
                        Presence presence = new Presence(Presence.Type.subscribe);
                         presence.setStatus("Online");
                         xmpp.sendPacket(presence);
                        System.out.println("PRE-CONNECTED");


                        try 
                            xmpp.connect();


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

                            e1.printStackTrace();
                        
                        try 
                            xmpp.login(fb.getAppId(), session.getAccessToken());



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

【讨论】:

以上是关于Facebook 聊天使用 xmpp 问题-NoSuchAlgorithmException: KeyStore jks implementation not found的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 聊天 XMPP 协议

Facebook XMPP 聊天连接

既然 Facebook 即将禁用 XMPP API,那么使用 Facebook 聊天 API 的替代方法是啥?

XMPP facebook 聊天权限与 PHP

XMPP facebook 与非朋友聊天

Facebook 聊天 API - XMPP 输入暂停通知