我正在为 FACEBOOK 创建 XMPP 客户端。 xmpp 连接错误.xmpp.login(apiKey + "|" + sessionKey, sessionSecret,

Posted

技术标签:

【中文标题】我正在为 FACEBOOK 创建 XMPP 客户端。 xmpp 连接错误.xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application");【英文标题】:I am creating XMPP client for FACEBOOK. xmpp connection is error.xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application"); 【发布时间】:2016-04-01 09:09:46 【问题描述】:

这是我迄今为止尝试过的。

public class MainActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback, GraphRequest.GraphJSONArrayCallback 
    private static final String TAG = "MainActivity";
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    SharedPreferences sharedpreferences;
    User user;
    ArrayList<HashMap<String, String>> friends_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        generateHashkey();
        user=new User();
        callbackManager = CallbackManager.Factory.create();
        sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile", "email", "user_friends");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
            @Override
            public void onSuccess(LoginResult loginResult) 
                Log.d(TAG, loginResult.toString());
                getUserDetails();
                getMyFriendsDetails();

            

            @Override
            public void onCancel() 
                Log.d(TAG, "cancel");
            

            @Override
            public void onError(FacebookException e) 
                Log.d(TAG, e.toString());
            
        );

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken != null) 
            getUserDetails();
            getMyFriendsDetails();
        
        Button bn=(Button)findViewById(R.id.button);
        bn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                new connect().execute();

            
        );
    

    private void getUserDetails() 

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken != null) 
            GraphRequest request = GraphRequest.newMeRequest(accessToken, this);
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name, first_name, last_name, email,gender, birthday, location");
            request.setParameters(parameters);
            request.executeAsync();
        
    

    private void getMyFriendsDetails() 
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken != null) 
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name, first_name, last_name, email,gender, birthday, location");
            GraphRequest request = GraphRequest.newMyFriendsRequest(accessToken, this);
            request.setParameters(parameters);
            request.executeAsync();
        
    


    /*private void getTaggableFriends()
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        Bundle bundle=new Bundle();
        bundle.putString("fields", "id, first_name, last_name, picture,email");
        if(accessToken!=null)
            GraphRequest graphRequest=new GraphRequest(accessToken,"/950173815028714/taggable_friends",null, HttpMethod.GET, new GraphRequest.Callback()
                public void onCompleted(GraphResponse response) 
                    Log.d("Taggable friends",response.toString());
                
            );
            graphRequest.setParameters(bundle);
            graphRequest.executeAsync();
        
    */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    

    private void generateHashkey() 
        try 
            PackageInfo info = getPackageManager().getPackageInfo("com.colors.fbfriends", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) 
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            
         catch (PackageManager.NameNotFoundException e) 

         catch (NoSuchAlgorithmException e) 

        
    

    @Override
    public void onCompleted(JSONArray jsonArray, GraphResponse graphResponse) 
        // Log.d("My friends:", jsonArray.toString());
    

    @Override
    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) 
        AccessToken accessToken = AccessToken.getCurrentAccessToken();

        Log.i("MainActivity",accessToken.getApplicationId());
        Log.i("MainActivity",accessToken.getToken());

        Log.i("MainActivity", accessToken.getUserId());

        Log.d("User Details:", jsonObject.toString());


        user.setAccessToken(accessToken.getToken());
        user.setApp_Id(accessToken.getApplicationId());
        user.setUserId(accessToken.getUserId());
    

    @Override
    protected void onResume() 
        super.onResume();
        AppEventsLogger.activateApp(this);
    

    @Override
    protected void onPause() 
        super.onPause();
        AppEventsLogger.deactivateApp(this);
    




    private class connect extends AsyncTask<Void, Void, Void> 



        @Override
        protected Void doInBackground(Void... voids) 
            ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
            config.setSASLAuthenticationEnabled(true);
            XMPPConnection xmpp = new XMPPConnection(config);
            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);
            
            try
            
                AccessToken accessToken = AccessToken.getCurrentAccessToken();

                SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
                SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
                xmpp.connect();
                xmpp.login("879963405446307" ,accessToken.getToken(), "application");
             catch (XMPPException e)
            
                xmpp.disconnect();
                e.printStackTrace();
            
             return null ;
        


    

    



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;
    

最后我得到那些错误 SASL 身份验证失败使用机制 X-FACEBOOK-PLATFORM:

    列表项

【问题讨论】:

Facebook 不是已经停止了它的 XMPP 界面吗? 您好,感谢您的回复,是的,Facebook 停止了其 XMPP 接口。这是链接 developers.facebook.com/docs/chat.can 您告诉我实现的替代方案。 我认为没有其他选择。 【参考方案1】:

没有解决您的问题,但是当我研究您的代码时,我发现一行错误。您不应该像这样授予多个权限:

loginButton.setReadPermissions("public_profile", "email", "user_friends");

将此行替换为:

loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday","user_friends"));

【讨论】:

您好,感谢您的回复。但我没有收到朋友数据 查看此链接并点击 Android-Sdk developers.facebook.com/docs/graph-api/reference/v2.2/user/…

以上是关于我正在为 FACEBOOK 创建 XMPP 客户端。 xmpp 连接错误.xmpp.login(apiKey + "|" + sessionKey, sessionSecret, 的主要内容,如果未能解决你的问题,请参考以下文章

在android中使用xmpp的android-facebook聊天客户端

使用 xmpp 的 Android-facebook 聊天客户端

如何创建多个 XMPP 客户端进行聊天?

WinRT 中的 Facebook 聊天 API/XMPP X-FACEBOOK-PLATFORM

来自资源的 Facebook XMPP IM 名称

XMPP Android - 构建客户端以在 facebook 上聊天或 facebook 聊天集成