2

我正在尝试在我的应用程序中使用 facebook 身份验证,但由于令牌丢失,我无法登录。在 LogCat 我有一个循环:“[Tag]System.out [Text]doLogin:”。这发生在 Facebook 对话框之前,当它出现时,我单击“确定”但没有任何反应。

这是我的代码:

private SharedPreferences.Editor loginPrefsEditor;
private Session.StatusCallback statusCallback = new SessionStatusCallback();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fb_auth);
    choosePseudoButton = (Button) findViewById(R.id.buttonChoosePseudoFb);
    progressBarFb = (ProgressBar) findViewById(R.id.progressBarFbLoad);
    textViewFbInfo = (TextView) findViewById(R.id.currentInfoFb);
    editTextFbPseudo = (EditText) findViewById(R.id.editTextPseudoFb);
    /*
     * Get existing access_token if any
     */
    mPrefs = getPreferences(MODE_PRIVATE);

    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {
        public void call(final Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            System.out.println("onCreate");  
                            fbToken = session.getAccessToken();
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token", fbToken);
                            JSONRequest jr = (JSONRequest) new JSONRequest().execute();

                        }
                    }
                });
            }
            else {
                doLogin();
            }
        }
    });
}

private void doLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(this, true, statusCallback);
    }
    fbToken = session.getAccessToken();
    System.out.println("doLogin: "+fbToken);
    JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}


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

private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        fbToken = session.getAccessToken();
        System.out.println("updateView: "+fbToken);
        JSONRequest jr = (JSONRequest) new JSONRequest().execute();
    } 
    else {
        doLogin();
    }
}

private class SessionStatusCallback implements Session.StatusCallback {
    public void call(Session session, SessionState state, Exception exception) {
        // Respond to session state changes, ex: updating the view
        if( session.isOpened() ){
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                public void onCompleted(GraphUser user, Response response) {
                }
            });
        }
        updateView();
    }
}
4

1 回答 1

1

需要注意的几点:

会话打开是异步发生的,这意味着当您调用 Session.openActiveSession 时,它不会(总是)立即打开会话(它必须调用另一个活动,等待用户输入等)。所以你的线

fbToken = session.getAccessToken();

几乎总是返回 null。

其次,在 Session 进入 OPENED 状态之前,它可能处于几种状态,因此在 SessionStatusCallback 中,您可能应该将 updateView() 调用移到 if(session.isOpened()) 检查中。

最后,您在 onCreate 方法中调用 Session.openActiveSession ,并且在该回调中,如果会话未打开,则始终调用 doLogin() 。但是就像我之前说的,会话在它打开之前可以转换到多个状态,所以你实际上是在多次调用 doLogin。删除该 doLogin() 调用。

于 2013-03-26T16:50:52.470 回答