Facebook authentication without a login button

I followed some of the Facebook 3.0 API tutorials, including login / logout examples and feed sharing examples. Thus, the login works as follows:

  • The application opens, shows a fragment that displays the login button
  • The user clicks the login, authentication is performed through the specified FacebookSDK library (com.facebook.widget.LoginActivity) and the provided code using sessions.
  • The user is redirected to the next screen.

I do not want the user to log in this way. I want them to use my application without registering / registering, then if they click on a special facebook function, for example. share the note on Facebook, then the application should ask them if Facebook allows you to use their application or something else, you know the usual things. Without this, I get nullpointer in the publishFeedDialog () function since the session is null because the input was not made.

So my question is: how can I ignore SplashFragment using the "Login" button, so when a user clicks on the Facebook function in my application, the new screen is not displayed using the login button, but only the default Facebook authentication window, which users are used?

+43
android facebook facebook-android-sdk
Aug 08 '13 at 20:52
source share
6 answers

@erdomester, @sromku

Facebook will launch a new version of sdk 4.x, where Session is deprecated,

There is a new concept of logging in as from facebook

LoginManager and AccessToken - These new classes perform Facebook login

So now you can access Facebook authentication without the 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 do not add the following, this will not work ( @Daniel Zolnai correctly pointed out in the comment below):

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) { return; } } 
+71
May 14 '15 at 6:27
source share
β€” -

Something like that

 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 the same. This works great for me.

+9
Aug 08 '13 at 22:32
source share

It 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 authorization after verifying that the session is open, add this method:

 List<String> permissions = session.getPermissions(); Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(getActivity(), Arrays.asList("read_mailbox")); session.requestNewReadPermissions(newPermissionsRequest); 
+3
Aug 18 '14 at 20:39
source share

This simple library can help you: https://github.com/sromku/android-simple-facebook

Just add this library to your project and make a link from this library to Facebook SDK 3.0.x and add a link from your application to this library.

You can then log in without LoginButton and perform simple actions, such as posting feeds, getting a profile / friends, sending an invitation, and much more.

Here is the login:

 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 the onLogin() callback method, you can publish the feed as follows:

 // 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 this helps you.

+2
Aug 09 '13 at 16:19
source share

A Turnaroubd to access FB data using LoginButton

1) Hide your LoginButton user interface

2) Add your custom button

 Button signup = (Button) view.findViewById(R.id.btn_signup); signup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loginButton.performClick();//Where loginButton is Facebook UI } }); 

But I suggest using LoginManager

+1
Jun 12 '16 at 6:08
source share

You can bypass the login dialog using Node powered by facebook-proxy . Create your own instance on Heroku using a one-click button.

What this basically does:

  • Request access_token from Facebook
  • Opens proxy server using express-http-proxy
  • Allows requesting all API endpoints
0
Nov 09 '17 at 22:18
source share



All Articles