0

我在我的应用程序中发布到 Facebook 粉丝页面,但我收到了我无法弄清楚的错误。

错误是:

java.lang.UnsupportedOperationException:会话:尝试为具有待处理请求的会话请求新权限。

我可以在我的墙上发布数据,但是当我想在我的应用程序的 Facebook 粉丝页面上发布数据时,我收到了上述错误。
代码片段是:

public class FacebookShareFragment extends Fragment {

    private ProgressDialog progressDialog;
    private UiLifecycleHelper uiHelper;
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","manage_pages","publish_stream");
    private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
    private boolean pendingPublishReauthorization = false;

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

 @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.facebook_share_fragment, container, false);
        if (savedInstanceState != null) {
            pendingPublishReauthorization =
                    savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
        }
 shareOnFanPage();
 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();
        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()) {
            if (pendingPublishReauthorization &&
                    state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
                pendingPublishReauthorization = false;
       //         publishStory();
                shareOnFanPage();
            }
        } else if (state.isClosed()) {
        }
    }

    /**
     *  Method to publish on the fan page
     */
    public void shareOnFanPage(){
        Session session = Session.getActiveSession();
        if (session != null) {
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest); // AT THIS POINT I AM GETTING THE ERROR.
                return;
            }

            // Show a progress dialog because the batch request could take a while.
            progressDialog = ProgressDialog.show(getActivity(), "",
                    getActivity().getResources().getString(R.string.progress_dialog_text), true);

            try {
                final RequestBatch requestBatch = new RequestBatch();
                requestBatch.add(fbFan());
                requestBatch.executeAsync();
            }catch(Exception e){
                    e.printStackTrace();
            }
        }
    }

首先,我能够在我的 FB 墙上执行发布数据但后来根据要求,我添加了新权限“manage_pages”和“publish_stream”,但不知道为什么即使我评论了 publishStory() 方法也会出现错误在我的 FB 墙上发布数据。

我读了很多帖子,但无法弄清楚......

4

1 回答 1

0

我相信 onCreateView 在 onActivityResult 之前被调用。在这种情况下,如果您的托管活动在权限请求完成之前被终止(或者如果您打开了“不保留活动”开发选项),那么此方法将始终在 onActivityResult 之前调用。

由于您还在 onCreateView 中调用了 shareOnFanPage,因此您实际上将在前一个权限请求完成之前发出另一个权限请求(因为当您将它传递给 uiLifecycleHelper 时,它已在 onActivityResult 中完成)。

我会将 shareOnFanPage() 调用移至 onResume,或者干脆将其完全删除,因为我认为您不需要它,因为您已经在 onResume 方法中调用 onSessionStateChanged。

于 2014-12-12T19:35:36.977 回答