Facebook authentication without login button

后端 未结 6 1209
Happy的楠姐
Happy的楠姐 2020-11-28 21:33

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 f
相关标签:
6条回答
  • 2020-11-28 21:58

    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.

    0 讨论(0)
  • 2020-11-28 22:06

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

    What it basicly does:

    1. Requests an access_token from Facebook
    2. Opens a proxy server using express-http-proxy
    3. Let's you request all endpoints of the API
    0 讨论(0)
  • 2020-11-28 22:08

    @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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 22:09

    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);
    
    0 讨论(0)
  • 2020-11-28 22:14

    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.

    0 讨论(0)
  • 2020-11-28 22:20

    A Turnaroubd to access FB details withot using LoginButton is

    1)Hide Your LoginButton UI

    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 to use LoginManager

    0 讨论(0)
提交回复
热议问题