在Facebook页面上发布为页面而不是用户

时间:2014-07-17 12:48:34

标签: java android facebook sdk

我试图以管理员身份发布到Facebook页面。帖子总是以用户身份显示,而不是帖子是按页面保存的。

这是代码,我在用户墙上发布了一个帖子样本并添加了manage_page权限并将我/ feed更改为page_id

Java文件:

public class MainFragment extends Fragment {

private static final String TAG = "MainFragment";

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

private Button shareButton;

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");


private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";

private boolean pendingPublishReauthorization = false;
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main, container, false);
    text = (TextView)view.findViewById(R.id.textView1);
    LoginButton authButton = (LoginButton) view
            .findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setPublishPermissions("manage_pages");
    shareButton = (Button) view.findViewById(R.id.shareButton);
    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            publishStory();
        }
    });

    if (savedInstanceState != null) {
        pendingPublishReauthorization = savedInstanceState.getBoolean(
                PENDING_PUBLISH_KEY, false);
    }
    return view;
}

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

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

@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 onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@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);
    outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        shareButton.setVisibility(View.VISIBLE);
        if (pendingPublishReauthorization
                && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
            pendingPublishReauthorization = false;
            publishStory();
        }
    } else if (state.isClosed()) {
        shareButton.setVisibility(View.INVISIBLE);
    }
}

private void publishStory() {
    final Session session = Session.getActiveSession();
    if (session != null) {

        // Check for publish permissions
        List<String> permissions = session.getPermissions();

        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                    this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);


        //  session.open(session.getAccessToken(), callback);
            return;
        }

        final Bundle postParams = new Bundle();
        postParams.putString("message", "Testing app");


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

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

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

private boolean isSubsetOf(Collection<String> subset,
        Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

显然您没有获取页面访问令牌,而是使用用户访问令牌。申请manage_pages权限是正确的,但之后您需要拨打电话

/me/accounts

获取个人网页的访问令牌。之后,您必须使用此访问令牌向

发出请求
{page_id}/feed