Facebook帖子没有显示

时间:2014-05-21 12:03:36

标签: android facebook

我在facebook开发者网站创建了一个应用程序并在我的应用程序中使用appId并尝试在Facebook上发布,当我在我的应用程序中使用相同的fb帐户登录并使用我的Facebook发布时,它发布成功,但是当我尝试使用其他帐户登录但不发布...可以任何身体帮助......

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.suppliers.bee2buy.R;
import com.suppliers.utils.AppPreferences;

public class ShareData extends Activity {
    private Session.StatusCallback sessionStatusCallback;
    private Session currentSession;
    Button publishButton;
    AppPreferences appPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sharedata);
        appPref = new AppPreferences(ShareData.this, "PREFS");
        publishButton = (Button) findViewById(R.id.publishButton);

        connectToFB();

        publishButton.setVisibility(View.VISIBLE);

        // create instace for sessionStatusCallback
        sessionStatusCallback = new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                onSessionStateChange(session, state, exception);

            }
        };

        // logout button

        // publish button
        publishButton = (Button) findViewById(R.id.publishButton);
        publishButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                publishStorys();

            }
        });

    }

    /**
     * Connects the user to facebook
     */
    public void connectToFB() {

        List<String> permissions = new ArrayList<String>();
        permissions.add("publish_stream");


        currentSession = new Session.Builder(this).build();
        currentSession.addCallback(sessionStatusCallback);

        Session.OpenRequest openRequest = new Session.OpenRequest(
                ShareData.this);
        openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        openRequest.setRequestCode(Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);
        openRequest.setPermissions(permissions);
        currentSession.openForPublish(openRequest);

    }

    /**
     * this method is used by the facebook API
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (currentSession != null) {
            currentSession
                    .onActivityResult(this, requestCode, resultCode, data);
        }
    }

    /**
     * manages the session state change. This method is called after the
     * <code>connectToFB</code> method.
     * 
     * @param session
     * @param state
     * @param exception
     */
    private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
        if (session != currentSession) {
            return;
        }

        if (state.isOpened()) {
            // Log in just happened.
            Toast.makeText(getApplicationContext(), "session opened",
                    Toast.LENGTH_SHORT).show();
        } else if (state.isClosed()) {
            // Log out just happened. Update the UI.
            Toast.makeText(getApplicationContext(), "session closed",
                    Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Publishes story on the logged user's wall
     */

    private void publishStorys() {
        if (currentSession != null) {
            // this is not used
            // Bitmap icon =
            // BitmapFactory.decodeResource(getApplicationContext()
            // .getResources(), R.drawable.ic_launcher);

            Bundle postParams = new Bundle();
            postParams.putString("name", "Product Shared from Bee2Buy");
            postParams.putString("caption", appPref.getData("share_title"));
            // postParams
            // .putString(
            // "description",
            // "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
            postParams.putString(
                    "link",
                    "http://www.saznaitsols.com/site_uploads/product/"
                            + appPref.getData("share_img"));
            postParams.putString(
                    "picture",
                    "http://www.saznaitsols.com/site_uploads/product/"
                            + appPref.getData("share_img"));

            Request.Callback callback = new Request.Callback() {
                public void onCompleted(Response response) {

                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(ShareData.this.getApplicationContext(),
                                error.getErrorMessage(), Toast.LENGTH_SHORT)
                                .show();
                    } else {
                        // Toast.makeText(
                        // ShareData.this.getApplicationContext(),
                        // postId, Toast.LENGTH_LONG).show();
                    }
                }
            };

            Request request = new Request(currentSession, "me/feed",
                    postParams, HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
            Toast.makeText(ShareData.this, "Posted on fb", Toast.LENGTH_SHORT).show();
            ShareData.this.finish();


        } else {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

2 个答案:

答案 0 :(得分:2)

如果您想将某些内容发布到用户的墙上,则应根据https://developers.facebook.com/docs/graph-api/reference/v2.0/user/feed/#publish申请publish_actions权限。

如果您使用不同的帐户,是否看到权限对话框?我怀疑你尝试使用它的第一个用户是你应用的管理员,而第二个用户不是。这就是为什么它适用于第一个。

答案 1 :(得分:0)

感谢Tobi的回复..我找到了另一个在facebook上分享数据的解决方案,使用Facebook团队现在推荐的提要对话框,如果你对Facebook上发布的内容有额外的对话,我们可以使用这个方法。但是请确保我们已经像Tobi所说的那样获得了所有许可。

   private void publishFeedDialog() {
            Bundle params = new Bundle();
            params.putString("name", "Product shared from Bee2buy");
            params.putString("caption",
                    appPref.getData("share_title"));

            params.putString("link", "http://www.saznaitsols.com/site_uploads/product/"
                    + appPref.getData("share_img"));
            params.putString("picture",
                    "http://www.saznaitsols.com/site_uploads/product/"
                            + appPref.getData("share_img"));

            WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
                    ShareData.this, currentSession, params))
                    .setOnCompleteListener(new OnCompleteListener() {

                        @Override
                        public void onComplete(Bundle values,
                                FacebookException error) {
                            if (error == null) {
                                // When the story is posted, echo the success
                                // and the post Id.
                                final String postId = values.getString("post_id");
                                if (postId != null) {
                                    Toast.makeText(ShareData.this,
                                            "Posted story, id: " + postId,
                                            Toast.LENGTH_SHORT).show();
                                } else {
                                    // User clicked the Cancel button
                                    Toast.makeText(
                                            ShareData.this
                                                    .getApplicationContext(),
                                            "Publish cancelled", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            } else if (error instanceof FacebookOperationCanceledException) {
                                // User clicked the "x" button
                                Toast.makeText(
                                        ShareData.this.getApplicationContext(),
                                        "Publish cancelled", Toast.LENGTH_SHORT)
                                        .show();
                            } else {
                                // Generic, ex: network error
                                Toast.makeText(
                                        ShareData.this.getApplicationContext(),
                                        "Error posting story", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        }



                    }).build();
            feedDialog.show();
        }