Facebook登录在授权时无效

时间:2015-04-20 04:18:58

标签: android facebook android-facebook

我正在尝试使用Login With Facebook来实施Facebook SDK 3.23.0。一切正常,直到用户已经授权Facebook并且用户已经登录Facebook Application。当我点击Log in With Facebook按钮如果用户已经在Facebook应用程序中签名以及已经获得授权许可,则不会向我显示任何对话框。

我无法在登录时解决Facebook的这种疲惫行为。

fbLoginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
        @Override
        public void onUserInfoFetched(GraphUser user) {
                    FirstScreenActivity.this.user = user;
                    updateUI();
                    // It's possible that we were waiting for this.user to
                    // be populated in order to post a
                    // status update.
                    handlePendingAction();

                }
        });


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

private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall,
                Exception error, Bundle data) {
            Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
            fbLoginButton.setVisibility(View.VISIBLE);
            registerButton.setVisibility(View.VISIBLE);
            loginButton.setVisibility(View.VISIBLE);
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall,
                Bundle data) {
            Log.d("HelloFacebook", "Success!");
            fbLoginButton.setVisibility(View.GONE);
            registerButton.setVisibility(View.GONE);
            loginButton.setVisibility(View.GONE);
        }
};

private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
        if (pendingAction != PendingAction.NONE
                && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
            new AlertDialog.Builder(FirstScreenActivity.this)
                    .setTitle(R.string.cancelled)
                    .setMessage(R.string.permission_not_granted)
                    .setPositiveButton(R.string.ok, null).show();
            pendingAction = PendingAction.NONE;
        } else if (state == SessionState.OPENED_TOKEN_UPDATED) {
            handlePendingAction();
        }
        updateUI();
}

private void updateUI() {
    Session session = Session.getActiveSession();
    boolean enableButtons = (session != null && session.isOpened());



    if ((enableButtons && user != null)) {
        // profilePictureView.setProfileId(user.getId());
        // greeting.setText(getString(R.string.hello_user,
        // user.getFirstName()));
        if((mStore_pref.get_fb_token() ==null) && TextUtils.isEmpty(mStore_pref.get_fb_token()))
            mStore_pref.set_fb_token(session.getAccessToken());

        fbLoginButton.setVisibility(View.GONE);
        registerButton.setVisibility(View.GONE);
        loginButton.setVisibility(View.GONE);
        Log.i("Birthday", user.getBirthday() + "");
        Log.i("ID", user.getId() + "");
        Log.i("Full Name", user.getName() + "");

        if (user.getId() != null && !TextUtils.isEmpty(user.getId())) {


            userID = user.getId();
            dob = user.getBirthday();
            if(dob!=null && !TextUtils.isEmpty(dob)){
                String[] datesDob = dob.split("/");
                if(datesDob.length==3){
                    dob = datesDob[2]+"/"+datesDob[0]+"/"+datesDob[1];
                }
            }


            Log.i("DOB after formatting", dob+" is dob");
            fullName = user.getName();
            email = user.asMap().get("email").toString();
            gender = user.asMap().get("gender").toString();


    } else if((mStore_pref.get_fb_token() !=null) && !TextUtils.isEmpty(mStore_pref.get_fb_token())){

        //Session.openActiveSession(FirstScreenActivity.this, true, callback);
        /*Request request = Request.newMeRequest(
                (Session)mStore_pref.get_fb_token() ,
                new GraphUserCallback() {                     

                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {
                        // TODO Auto-generated method stub

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link");
        request.setParameters(parameters);
        request.executeAsync();*/

    }else{
                    fbLoginButton.setVisibility(View.VISIBLE);
                    registerButton.setVisibility(View.VISIBLE);
                    loginButton.setVisibility(View.VISIBLE);
    }
}

@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
        PendingAction previouslyPendingAction = pendingAction;
        // These actions may re-set pendingAction if they are still pending, but
        // we assume they
        // will succeed.
        pendingAction = PendingAction.NONE;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) {
            Session.getActiveSession().onActivityResult(this, requestCode,
                    resultCode, data);
            Log.i("Facebook", "Request code");
            uiHelper.onActivityResult(requestCode, resultCode, data,
                    dialogCallback);

            fbLoginButton.setVisibility(View.GONE);
            registerButton.setVisibility(View.GONE);
            loginButton.setVisibility(View.GONE);
        } 
}

0 个答案:

没有答案