Facebook authentication without login button
@erdomester, @sromku
Facebook launch new sdk version 4.x where Session is deprecated,
There new concept of login as from facebook
LoginManager and AccessToken - These new classes perform Facebook Login
So, Now you can access Facebook authentication without login button as
layout.xml
<Button
android:id="@+id/btn_fb_login"
.../>
MainActivity.java
private CallbackManager mCallbackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
setContentView(R.layout.activity_main);
Button btn_fb_login = (Button)findViewById(R.id.btn_fb_login);
btn_fb_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
});
}
Edit
If you don't add the following, it won't work (rightly pointed out by @Daniel Zolnai in comment below):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
Something like this
private void performFacebookLogin()
{
Log.d("FACEBOOK", "performFacebookLogin");
final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, Arrays.asList("email"));
Session openActiveSession = Session.openActiveSession(this, true, new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
Log.d("FACEBOOK", "call");
if (session.isOpened() && !isFetching)
{
Log.d("FACEBOOK", "if (session.isOpened() && !isFetching)");
isFetching = true;
session.requestNewReadPermissions(newPermissionsRequest);
Request getMe = Request.newMeRequest(session, new GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
Log.d("FACEBOOK", "onCompleted");
if (user != null)
{
Log.d("FACEBOOK", "user != null");
org.json.JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String email = graphResponse.optString("email");
String id = graphResponse.optString("id");
String facebookName = user.getUsername();
if (email == null || email.length() < 0)
{
Logic.showAlert(
ActivityLogin.this,
"Facebook Login",
"An email address is required for your account, we could not find an email associated with this Facebook account. Please associate a email with this account or login the oldskool way.");
return;
}
}
}
});
getMe.executeAsync();
}
else
{
if (!session.isOpened())
Log.d("FACEBOOK", "!session.isOpened()");
else
Log.d("FACEBOOK", "isFetching");
}
}
});
Actually exactly like that. It works perfectly fine for me.
This worked for me
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.*;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
}).executeAsync();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
if you need to get authorizations after verify that session is open ,add this way:
List<String> permissions = session.getPermissions();
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(getActivity(), Arrays.asList("read_mailbox"));
session.requestNewReadPermissions(newPermissionsRequest);
This simple library can help you: https://github.com/sromku/android-simple-facebook
Just add this library to your project and make the reference from this library to Facebook SDK 3.0.x and add reference from your app to this library.
Then you can login without the LoginButton
and do simple actions like publish feeds, get profile/friends, send invite and more.
This is how the login look like:
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 login/logut listener
mSimpleFacebook.setLogInOutListener(onLoginOutListener);
// do the login action
mSimpleFacebook.login(MainActivity.this);
Then, in onLogin()
callback method you can publish feed like this:
// build feed
Feed feed = new Feed.Builder()
.setMessage("Clone it out...")
.setName("Simple Facebook for Android")
.setCaption("Code less, do the same.")
.setDescription("The Simple Facebook library project makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.")
.setPicture("https://raw.github.com/sromku/android-simple-facebook/master/Refs/android_facebook_sdk_logo.png")
.setLink("https://github.com/sromku/android-simple-facebook")
.build();
// publish the feed
mSimpleFacebook.publish(feed);
Hope it can help you.