k_day
k_day

Reputation: 1379

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.

Upvotes: 18

Views: 13533

Answers (2)

Rowan
Rowan

Reputation: 163

@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.

Upvotes: 1

C Abernathy
C Abernathy

Reputation: 5523

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

Upvotes: 69

Related Questions