I am writing an app that integrates with Facebook SDK, to share some (string) content as a wall post. Now, I made the HelloFacebookSample work. However It uses their LoginBu
Since I had the same feeling as many here who upvoted the @Beppi's comment to @Ming Li answer, and since I use Facebook SDK in my apps, I decided to create more simplified API level which is based on latest Facebook SDK 3.0.b.
The open source library: android-simple-facebook
https://github.com/sromku/android-simple-facebook
To your question: How to login programatically?
Set login/logout listener
// set login / logout listener
OnLoginOutListener onLoginOutListener = new SimpleFacebook.OnLoginOutListener()
{
@Override
public void onFail()
{
Log.w(TAG, "Failed to login");
}
@Override
public void onException(Throwable throwable)
{
Log.e(TAG, "Bad thing happened", throwable);
}
@Override
public void onThinking()
{
// show progress bar or something to the user while login is happening
Log.i(TAG, "In progress");
}
@Override
public void onLogout()
{
// change the state of the button or do whatever you want
Log.i(TAG, "Logged out");
}
@Override
public void onLogin()
{
// change the state of the button or do whatever you want
Log.i(TAG, "Logged in");
}
};
// set the listener
mSimpleFacebook.setLogInOutListener(onLoginOutListener);
On click of any view, just call login(Activity) method
mSimpleFacebook.login(MainActivity.this);
To logout call logout() method. Like this:
mSimpleFacebook.logout();
How to set permissions before login, see very friendly explanation here.
Hope it could be helpful to someone :)
Great piece of code, thanks.
Please note that with v3 SDK version, the reauthorize code must be replaced with:
Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(FacebookActivity.this, PERMISSIONS)
.setRequestCode(REAUTHORIZE_ACTIVITY)
.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.requestNewPublishPermissions(reauthRequest);
Is what you posted your entire Activity?
You also need to override onActivityResult, and pass the values to Session.getActiveSession().onActivityResult(...). Otherwise, the Session won't know that the user has authorized your app, and that's why you see the error (Session thinks that there's still a pending auth request, which is why you can't reauthorize for publish).