Facebook authentication without login button

后端 未结 6 1216
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 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.

提交回复
热议问题