Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK3.0, session state OPENING

I am trying to use the Facebook SDK 3.0 to retrieve an access token on button press in my android app. I have a generic button in my Activity that is doing the following:

Button facebook = (Button)findViewById(R.id.facebookLoginButton);
    facebook.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Session session = new Session(getApplicationContext());
            Session.setActiveSession(session);
            session.openForRead(new Session.OpenRequest(SignInActivity.this).setCallback(statusCallback));
        }
    });

And then the callback:

private class FacebookSessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
            String s=session.getAccessToken();
    }
}

Clicking the button prompts me for my permission to access my profile as expected, but this callback is only ever called once with SessionState as "OPENING". The state doesn't change after this.

What am I missing here? My end goal is really just to get an access token once, and I don't really care about persisting the session or using it to log into my app.

like image 709
k_day Avatar asked Dec 14 '12 20:12

k_day


2 Answers

You need to override the onActivityResult so that after the credentials are checked against Facebook for Android (or an inline login) the results are handled and the session updated. Add the following code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession()
        .onActivityResult(this, requestCode, resultCode, data);
}
like image 147
C Abernathy Avatar answered Nov 07 '22 23:11

C Abernathy


@Lucas Jota: Try changing request code. this works for me. session.openForRead(new Session.OpenRequest(LoginActivity.this).setCallback(statusCallback).setRequestCode(urRequestCode)); Also, assure that your activity doesn't have "single instance" as property.

like image 30
Rowan Avatar answered Nov 07 '22 22:11

Rowan