Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook authentication without login button

I have followed some Facebook API 3.0 tutorials, including the Login/Logout and the Publish To Feed examples. So the login works this way:

  1. App opens, shows a fragment which displays a login button
  2. User clicks login, the authentication is done via the referenced FacebookSDK library (com.facebook.widget.LoginActivity) and the provided code using sessions.
  3. User is redirected to next screen

I don't want to make the user to login this way. I want them to use my app without any login/registration, then if they click on a facebook specific feature e.g. share a note on Facebook, then the app should ask them whether they let Facebook use their app or something, you know the usual stuff. Without this I get a nullpointer in the publishFeedDialog() function as session is null, because no login has been made.

So my question is, how can I ignore the SplashFragment with the Login button, so when the user clicks on a Facebook feature in my app, no new screen is displayed with a login button, but only the default Facebook authentication window that users are used to?

like image 315
erdomester Avatar asked Aug 08 '13 20:08

erdomester


People also ask

What is the log back in button on Facebook?

The Login button is a simple way to trigger the Facebook Login process on your website or web app. If someone hasn't logged into your app yet, they'll see this button, and clicking it will open a Login dialog, starting the login flow.

How do I check my login activity on Facebook?

Tap in the top right of Facebook, then tap your name. Tap below your profile picture, then tap Activity Log. Tap View Activity History at the bottom.


1 Answers

@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;     } } 
like image 55
TejaDroid Avatar answered Oct 22 '22 21:10

TejaDroid