facebook android sdk - 只有开发者帐户可以共享帖子,但其他用户不能

时间:2013-06-23 12:21:19

标签: android facebook-android-sdk

我正在尝试通过以下代码在用户Feed中分享链接,只有我使用开发者帐户登录才有效。如果我尝试通过任何其他用户帐户发布它,它只会提供一个帖子ID,但没有可见的帖子。

MainFragment仅包含布局inflater,此布局只有TextView和facebook LoginButton

package com.chamika.fbtest;

import java.util.Arrays;
import java.util.List;

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

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
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.SessionDefaultAudience;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

public class MainActivity extends FragmentActivity {

    private UiLifecycleHelper uiHelper;
    private Context context;

    private static final String TAG = "MainFragment";

    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","publish_stream");
    private static final int REAUTH_ACTIVITY_CODE = 100;

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private MainFragment mainFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        context = this;

        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        // Session.getActiveSession().onActivityResult(this, requestCode,
        // resultCode, data);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        final TextView text = (TextView) findViewById(R.id.welcome);
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            text.setText("Logged in...");
            text.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    publish();
                }
            });

        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
            text.setText("Logged out...");
            text.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    text.setText("Please log");
                }
            });
        }
    }

    private void publish() {
        Session session = Session.getActiveSession();

        if (session == null || !session.isOpened()) {
            return;
        }

        List<String> permissions = session.getPermissions();
        if (!permissions.containsAll(PERMISSIONS)) {
            requestPublishPermissions(session);
            return;
        }

        // Show a progress dialog because sometimes the requests can take a
        // while.
        final ProgressDialog progressDialog = ProgressDialog.show(this, "requesting permission",
                "requesting additional permission", true);

        Bundle postParams = new Bundle();
        postParams.putString("name", "Test post");
        postParams.putString("caption", "This is a test post");
        postParams.putString("description",
                "Description of the test post");
        postParams.putString("link", "https://developers.facebook.com/android");
        postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {
                progressDialog.dismiss();
                JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                    Log.d(TAG, "postId=" + postId);
                } catch (JSONException e) {
                    Log.i(TAG, "JSON error " + e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(context.getApplicationContext(), error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context.getApplicationContext(), postId, Toast.LENGTH_LONG).show();
                }
            }
        };

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();

    }

    private void requestPublishPermissions(Session session) {
        if (session != null) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS)
            // demonstrate how to set an audience for the publish permissions,
            // if none are set, this defaults to FRIENDS
                    .setDefaultAudience(SessionDefaultAudience.FRIENDS).setRequestCode(REAUTH_ACTIVITY_CODE);
            session.requestNewPublishPermissions(newPermissionsRequest);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        // Session session = Session.getActiveSession();
        // if (session != null && (session.isOpened() || session.isClosed())) {
        // onSessionStateChange(session, session.getState(), null);
        // }
        uiHelper.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

}

4 个答案:

答案 0 :(得分:2)

默认情况下,应用处于沙盒模式(仅对管理员,开发人员和测试人员可见)

enter image description here

禁用沙盒模式。(向所有用户显示)

enter image description here

答案 1 :(得分:0)

我假设您可能忘记将测试者权限添加到您尝试过的除开发者帐户之外的Facebook帐户。当您开发应用程序时,它将处于沙盒模式,只有开发人员帐户和测试人员帐户才能访问。访问developer.facebook.com中的应用程序并添加测试人员帐户。

答案 2 :(得分:0)

您必须添加显式共享参数(fb:explicitly_shared)。

正如this page所述:“明确分享的行为有资格在新闻Feed中显示为独立的故事”。
这个东西适用于Open Graph规范,但我认为这在你的情况下也是有效的。

答案 3 :(得分:0)

Bundle params = new Bundle();
    params.putString("name", "Your App Name or any desired name");
    params.putString("description", "Message to share");
    params.putString("picture", "Image Url");

    WebDialog feedDialog = (
        new WebDialog.FeedDialogBuilder(context,
            Session.getActiveSession(),
            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) {
                        FacebookHelper.this.mShareDelegate
                        .shareStatus();
                    } else {
                        // User clicked the Cancel button

                    }
                } else if (error instanceof FacebookOperationCanceledException) {
                    // User clicked the "x" button

                } else {
                    // Generic, ex: network error

                }
            }

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