1

我在 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(), session.getState().toString());
        Log.e(getClass().getName(), "13");

        // updateView();
    }
}

在 SessionStatusCallback 类中,它总是首先将会话状态显示为 OPENING,然后显示为 CLOSED_LOGIN_FAILED。为什么状态变为 CLOSED_LOGIN_FAILED。

日志输出:

08-12 17:56:31.928: E/com.example.usemeone.UsemeOne(966): 0
08-12 17:56:31.948: E/com.example.usemeone.UsemeOne(966): 1
08-12 17:56:31.948: E/com.example.usemeone.UsemeOne(966): 3
08-12 17:56:31.988: E/com.example.usemeone.UsemeOne(966): 4
08-12 17:56:31.998: E/com.example.usemeone.UsemeOne(966): 6
08-12 17:56:31.998: E/com.example.usemeone.UsemeOne(966): 9
08-12 17:56:32.157: E/com.example.usemeone.UsemeOne(966): AT : 
08-12 17:56:32.398: E/com.example.usemeone.UsemeOne$2(966): 10
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$2(966): 11
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): OPENING
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): 13
08-12 17:57:26.468: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): CLOSED_LOGIN_FAILED
08-12 17:57:26.468: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): 13

它询问用户是否要允许该应用访问他/她的 Facebook 个人资料,但在授权该应用后,它会将会话状态更改为 CLOSED_LOGIN_FAILED。

4

0 回答 0