Reputation: 2712
I have a problem with integrating the Feed Dialog of the new Facebook SDK 3.0 for Android. The documentation is pretty clear about how to do it, however omitting one very important case, which is only mentioned as if it'll almost never happen.
Facebook Feed Dialog Documentation
This is the case when the user wants to publish something, but he is not already logged in and does not have Session and Access Token.
The SDK provides a way to set Access Token (and Session as whole) in the old Facebook object, however I cannot find a way to get the Session or Access Token after the user has logged in and posted his feed via the Feed dialog.
So the flow is:
facebook.dialog()
is started and the login dialog is presentedMy question is, how can I get the access token in step 5 (which is created somewhere for sure) and use it in my program to create a valid SDK 3.0 Session object, so that the user can have valid Session with Access Token after using the facebook dialog?
--- EDIT ---
I just want to clarify that the case is, when the native facebook app is not installed on the phone - therefore a webview dialog is used I guess.
Upvotes: 1
Views: 3191
Reputation: 1693
If you set an opened Session on the Facebook class, dialog() should not prompt the user to log in.
Here is an example:
public class MainActivity extends Activity {
boolean pendingShare;
Session session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null) {
pendingShare = savedInstanceState.getBoolean("pendingShare");
session = Session.restoreSession(this, null, new SessionCallback(), savedInstanceState);
}
((Button) findViewById(R.id.share_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickShare();
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("pendingShare", pendingShare);
Session.saveSession(session, outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (session != null) {
session.onActivityResult(this, requestCode, resultCode, data);
}
}
private void onClickShare() {
pendingShare = true;
session = new Session(MainActivity.this);
session.openForRead(new Session.OpenRequest(MainActivity.this).setCallback(new SessionCallback()));
}
private void share(Session session) {
Facebook facebook = new Facebook(session.getApplicationId());
facebook.setSession(session);
facebook.dialog(this, "feed", null);
pendingShare = false;
}
class SessionCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened() && pendingShare) {
share(session);
}
}
}
}
Saving/restoring "pendingShare" (and session) is useful here because your Activity can be unloaded during login, this remembers to trigger the dialog() call when MainActivity is reloaded.
Upvotes: 6