Facebook SDK 3.0 Android - 令牌丢失

时间:2013-03-25 12:48:13

标签: android facebook facebook-android-sdk

我正在尝试在我的应用中使用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();
    }
}

1 个答案:

答案 0 :(得分:1)

有几点需要注意:

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

fbToken = session.getAccessToken();

几乎总是会返回null。

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

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