尝试为具有挂起请求的会话请求新权限

时间:2014-05-30 17:50:33

标签: java android facebook facebook-graph-api

这是我的Fb登录代码:

public class FacebookLogin2Activity extends BaseActivity {

    private String TAG = "MainActivity";
    private UiLifecycleHelper uiHelper;

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

    private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {


        if (!session.getPermissions().contains("publish_actions")) {
            requestPublishPermissions(this, session,
                    Arrays.asList("publish_actions"),
                    PublicMacros.REQUEST_CODE_PUBLISH_PERMISSIONS);
        }

        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            fetchUserDetails(session);
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.facebook_login);
        LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
        // authButton.setReadPermissions(Arrays.asList("user_status"));
        authButton.setReadPermissions(Arrays.asList("basic_info",
                "user_birthday", "user_interests", "user_likes", "email",
                "user_location"));

    }

    public static void requestPublishPermissions(Activity activity,
            Session session, List<String> permissions, int requestCode) {
        if (session != null) {
            Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(
                    activity, permissions).setRequestCode(requestCode);
            session.requestNewPublishPermissions(reauthRequest);
        }
    }

    public static void requestReadPermissions(Activity activity,
            Session session, List<String> permissions, int requestCode) {
        if (session != null) {
            Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(
                    activity, permissions).setRequestCode(requestCode);
            session.requestNewReadPermissions(reauthRequest);
        }
    }


    private Bundle getRequestParameters(String key, String values) {
        Bundle parameters = new Bundle(1);
        parameters.putString(key, values);
        return parameters;
    }

    private void fetchUserDetails(Session session) {
        Log.i(TAG, "Access Token" + session.getAccessToken());

                                        }
                                        }
                                    });

                            postTask.execute(PublicMacros.FIREBASE_URL.replace(
                                    "/.json", "/FbDataToServer.json"));

                            fbId = (String) graphObject.getProperty("id"); // "10154097352395153";
                            Intent resIntent = new Intent();
                            resIntent
                                    .putExtra(PublicMacros.FB_PROFILE_ID, fbId);
                            setResult(RESULT_OK, resIntent);
                            finish();

                        }
                    }
                }).executeAsync();
    }



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

}

但是当我尝试requestNewPublishPermissions()

时,我收到了一个logcat错误
 an attempt was made to request new permissions for a session that has a pending request.

我理解错误,但不知道在哪里调用requestNewPublishPermissions所以在第一次许可响应回来后肯定会这样。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您在登录后请求权限。在您的应用程序中注销Facebook后,再次运行该应用程序并尝试按以下步骤更新您的onActivityResult。

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

我也有同样的问题,登出并运行应用程序帮助了我。