如何在Facebook上更新状态

时间:2014-08-01 05:42:19

标签: android facebook

我想通过app更新facebook的状态,所以我搜索了很多但是无法在facebook上更新状态,我已经导入了facebook sdk3.15.0,它在logcat中给出了权限错误。让我知道它是怎么回事可以更新状态,它给出响应null,我的代码是

  imgfacebook.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    facebook = new Facebook(APP_ID);
                    mAsyncRunner = new AsyncFacebookRunner(facebook);
                    loginToFacebook();
                }
            });


    public void loginToFacebook() {
            mPrefs = getPreferences(MODE_PRIVATE);
            String access_token = mPrefs.getString("access_token", null);
            long expires = mPrefs.getLong("access_expires", 0);

            if (access_token != null) {
                facebook.setAccessToken(access_token);
                postToWall(CheckRating);
            }

            if (expires != 0) {
                facebook.setAccessExpires(expires);
            }

            if (!facebook.isSessionValid()) {
                facebook.authorize(this,
                        new String[] { "email", "publish_stream" },
                        new DialogListener() {

                            @Override
                            public void onCancel() {
                                // Function to handle cancel event
                            }

                            @Override
                            public void onComplete(Bundle values) {
                                // Function to handle complete event
                                // Edit Preferences and update facebook acess_token
                                SharedPreferences.Editor editor = mPrefs.edit();
                                editor.putString("access_token",
                                        facebook.getAccessToken());
                                editor.putLong("access_expires",
                                        facebook.getAccessExpires());
                                editor.commit();
                                postToWall(CheckRating);
                            }

                            @Override
                            public void onError(DialogError error) {
                                // Function to handle error

                            }

                            @Override
                            public void onFacebookError(FacebookError fberror) {
                                // Function to handle Facebook errors

                            }

                        });

public void postToWall(final String msg) {

        try {
            // Bundle parameters = new Bundle();
            // parameters.putString("message", "Text is lame. Listen up:");
            // parameters.putString("name", "Name");
            // parameters.putString("link", "http://www.google.com");
            // parameters.putString("caption", "Caption");
            // parameters.putString("description", "Description");
            //
            // String response = facebook.request("me/feed", parameters,
            // "POST");
            // Log.v("response", response);
            Bundle parameters = new Bundle();
            parameters.putString("message", "Text is lame. Listen up:");
            parameters.putString("method", "stream.publish");
            String response = facebook.request(parameters);
            Log.v("response", response);
            System.out.println("Response is ....." + response);
        } catch (Exception e) {
            System.out.println("Response is nulll......");
        }
        // post on user's wall.

    }

0 个答案:

没有答案