简体   繁体   English

Facebook SDK 3.0 Android-令牌丢失

[英]Facebook SDK 3.0 Android - Token missing

I'm trying to use the facebook authentification in my app but I'm not able to login because the token is missing. 我正在尝试在我的应用程序中使用Facebook身份验证,但由于缺少令牌而无法登录。 In the LogCat I have a loop : "[Tag]System.out [Text]doLogin:". 在LogCat中,我有一个循环:“ [Tag] System.out [Text] doLogin:”。 This happens before the Facebook dialog, and when it appears I click on "Ok" but nothing happens. 这发生在Facebook对话框之前,当我单击“确定”时,什么也没发生。

Here is my code: 这是我的代码:

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();
    }
}

Several things to note: 需要注意的几件事:

Session opening happens asynchronously, meaning that when you call Session.openActiveSession, it does not (always) immediately open the session (it has to call into another activity, wait for user input, etc). 会话打开是异步发生的,这意味着当您调用Session.openActiveSession时,它不会(总是)立即打开会话(它必须调用另一个活动,等待用户输入等)。 So your line 所以你的线

fbToken = session.getAccessToken();

will almost always return null. 几乎总是返回null。

Secondly, there are a few states a Session can be in before it gets to the OPENED state, so in your SessionStatusCallback, you should probably move the updateView() call to be inside the if(session.isOpened()) check. 其次,会话在进入OPENED状态之前可能处于几种状态,因此在SessionStatusCallback中,您可能应该将updateView()调用移至if(session.isOpened())检查中。

Lastly, you're calling Session.openActiveSession in the onCreate method, and in that callback, you're always calling doLogin() if the session is not opened. 最后,您在onCreate方法中调用Session.openActiveSession,在该回调中,如果未打开会话,则始终在调用doLogin()。 But like I said before, the session can transition to multiple states before it's OPENED, so you're in fact calling doLogin multiple times. 但是就像我之前说的,会话可以在打开之前转换到多个状态,因此实际上您要多次调用doLogin。 Remove that doLogin() call. 删除该doLogin()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM