android - session : an attempt was made to request new permissions for a session that has a pending request

标签 android facebook facebook-android-sdk

我想通过 Android 应用发布故事,我使用下面的代码。

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(XXActivity.this, callback);
        uiHelper.onCreate(savedInstanceState);
        setContentView(R.layout.layoutAct);
            ...
            ...
            ...
        publishAddCardStory();  
            ...         
    }

    @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);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

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

    @Override
    protected 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 publishAddCardStory() {
        Session session = initFacebookSession(PopupAddCardSuccessActivity.this);
        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);
                return;
            }

            Bundle params = new Bundle();
            ...
            params.putString("message", msg);
            params.putString("description", desc);
            params.putString("link", LINK);
                    ...


            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("FB-Response",
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(XXActivity.this
                             .getApplicationContext(),
                             getString(R.string.fb_post_fail),
                             Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(XXActivity.this
                                 .getApplicationContext(), 
                                 getString(R.string.fb_post_success),
                                 Toast.LENGTH_LONG).show();
                    }
                }
            };

            Request request = new Request(session, "me/feed", params, 
                                  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;
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            //shareButton.setVisibility(View.VISIBLE);
        } else if (state.isClosed()) {
            //shareButton.setVisibility(View.INVISIBLE);
        }
    }


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

    private static Session initFacebookSession(Context context) {
        Session session = Session.getActiveSession();
        if (session != null) 
            return session;

        if (session == null)
            session = new Session(context);
        return session;
    }

有时我会遇到这两个错误....

java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.

或者某个时候...

java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that is not currently open.

如有任何建议,我们将不胜感激。

最佳答案

首先,您的 initFacebookSession 方法没有用,因为您在其下方调用了 Session.getActiveSession(),因此您可以删除该方法。

接下来,您应该在 onSessionStateChange 方法中处理 SessionState 的变化。虽然您不这样做并且只在调用 publishAddCardStory 方法的地方位于 onCreate 中,但我认为您收到错误的原因是:

  1. 在onCreate中调用publish方法
  2. 在发布方法中,您通过 Session 调用 requestNewPermissions,因此您的 Activity 将被销毁,并且带有请求允许用户新权限的对话框的 Facebook Activity 出现并且 Session 进入挂起状态
  3. 用户将通过对话框允许(或不允许)新权限
  4. 您的 Activity 再次创建,因此当 Session 仍处于挂起状态且尚未调用 onSessionStateChange 时,您再次通过 Session 调用 newPermissionsRequest

尝试查看 Facebook 开发教程:https://developers.facebook.com/docs/android/scrumptious/ 如何处理 SessionState 的变化。 Facebook SDK 还提供了一些可能也有帮助的示例应用程序。祝你好运! :)

关于android - session : an attempt was made to request new permissions for a session that has a pending request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932885/

相关文章:

java - Android Facebook 登录 API 仅返回名称和 ID

java - 如何从 Android 中的 bundle 中提取值

Android:从另一个应用程序读取应用程序的设置

android - 我的 Android 应用程序已成功同步,但在运行该应用程序时出现错误任务 ':app:mergeDebugResources' 执行失败

facebook - 具有共享/喜欢/评论功能的社交网站的 mongodb 模式?

iphone - 新的 fbconnect Api 错误?

android - 三星移动 mtp 设备安装失败

java - OpenGL ES 中的纹理质量差

java - 如何使用 selenium webdriver 切换到重定向 url

android - 适用于 Android 的 Facebook SDK - 以编程方式设置应用程序 ID