0

我在 UI 中创建了一个按钮,单击该按钮应调用一个函数(fbLogin()),该函数会打开一个会话(如果已关闭)并授权用户。

public void fbLogin(View button) {
    Log.e(getClass().getName(), "0");
    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    Session session = Session.getActiveSession();
    if (session == null) {
        Log.e(getClass().getName(), "1");
        if (savedInstanceState != null) {
            Log.e(getClass().getName(), "2");
            session = Session.restoreSession(this, null, statusCallback,
                    savedInstanceState);
        }
        if (session == null) {
            Log.e(getClass().getName(), "3");
            session = new Session(this);
        }
        Log.e(getClass().getName(), "4");
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            Log.e(getClass().getName(), "5");
            session.openForRead(new Session.OpenRequest(this)
                    .setCallback(statusCallback));
        }
    }
    Log.e(getClass().getName(), "6");
    updateView();
}

更新视图():

private void updateView() {
    final Session session = Session.getActiveSession();
    if (session.isOpened()) {
        Log.e(getClass().getName(), "7");

        Request.executeMeRequestAsync(session,
                new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {
                        Log.e(getClass().getName(), "8");
                        if (user != null) {
                            String str = user.getName();
                            Intent intent = new Intent(
                                    getApplicationContext(), LoggedIn.class);
                            Log.e(getClass().getName(), user.getName());
                            intent.putExtra("UserName", str);
                            startActivity(intent);
                        }
                    }
                });

    }

    else {
        Log.e(getClass().getName(), "9");
        Session.openActiveSession(this, true, statusCallback);
                    if(session.isOpened()) Log.e(getClass().getName(), "15");
        Log.e(getClass().getName(), "AT : "+ session.getAccessToken());
        Request.executeMeRequestAsync(session,
                new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {
                        Log.e(getClass().getName(), "10");

                        if(user==null) Log.e(getClass().getName(), "11");
                        if (user != null) {
                            Log.e(getClass().getName(), "12");
                            String str = user.getName();
                            Intent intent = new Intent(
                                    getApplicationContext(), LoggedIn.class);
                            Log.e(getClass().getName(), user.getName());
                            intent.putExtra("UserName", str);
                            startActivity(intent);
                        }
                    }
                });

    }

}

我的 SessionStatusCallback 类:

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        Log.e(getClass().getName(), "13");

        // updateView();
    }
}

但是当我点击登录按钮时,活动继续加载(好像它正在获取令牌),但会话永远不会打开(因为记录器永远不会在 LogCat 中记录 15)

日志输出:

08-12 16:11:35.836: E/com.example.usemeone.UsemeOne(1725): 0
08-12 16:11:35.846: E/com.example.usemeone.UsemeOne(1725): 1
08-12 16:11:35.846: E/com.example.usemeone.UsemeOne(1725): 3
08-12 16:11:35.886: E/com.example.usemeone.UsemeOne(1725): 4
08-12 16:11:35.896: E/com.example.usemeone.UsemeOne(1725): 6
08-12 16:11:35.896: E/com.example.usemeone.UsemeOne(1725): 9
08-12 16:11:36.046: E/com.example.usemeone.UsemeOne(1725): AT : 
08-12 16:11:36.366: E/com.example.usemeone.UsemeOne$SessionStatusCallback(1725): 13
08-12 16:11:37.196: E/com.example.usemeone.UsemeOne$2(1725): 10
08-12 16:11:37.196: E/com.example.usemeone.UsemeOne$2(1725): 11
4

1 回答 1

0

onActivityResult()您是否正在实施Session?否则它不会完成身份验证。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Session.getActiveSession() != null)
            Session.getActiveSession().onActivityResult(this, requestCode,
                    resultCode, data);

        Session currentSession = Session.getActiveSession();
        if (currentSession == null || currentSession.getState().isClosed()) {
            Session session = new Session.Builder(this).build();
            Session.setActiveSession(session);
            currentSession = session;
        }

        if (currentSession.isOpened()) {
            Session.openActiveSession(this, true, statusCallback);
        }
    }
}
于 2013-08-12T16:39:41.253 回答