Android - Facebook SDK 3 - How to login programmatically without LoginButton

后端 未结 3 1480
难免孤独
难免孤独 2020-12-24 01:45

I am writing an app that integrates with Facebook SDK, to share some (string) content as a wall post. Now, I made the HelloFacebookSample work. However It uses their LoginBu

相关标签:
3条回答
  • 2020-12-24 01:51

    Since I had the same feeling as many here who upvoted the @Beppi's comment to @Ming Li answer, and since I use Facebook SDK in my apps, I decided to create more simplified API level which is based on latest Facebook SDK 3.0.b.

    The open source library: android-simple-facebook
    https://github.com/sromku/android-simple-facebook

    To your question: How to login programatically?

    1. Set login/logout listener

      // set login / logout listener
      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 the listener
      mSimpleFacebook.setLogInOutListener(onLoginOutListener);
      
    2. On click of any view, just call login(Activity) method

      mSimpleFacebook.login(MainActivity.this);
      


    3. To logout call logout() method. Like this:

      mSimpleFacebook.logout();
      

    How to set permissions before login, see very friendly explanation here.

    Hope it could be helpful to someone :)

    0 讨论(0)
  • 2020-12-24 02:04

    Great piece of code, thanks.

    Please note that with v3 SDK version, the reauthorize code must be replaced with:

    Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(FacebookActivity.this, PERMISSIONS)
                                    .setRequestCode(REAUTHORIZE_ACTIVITY)
                                    .setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                            session.requestNewPublishPermissions(reauthRequest);
    
    0 讨论(0)
  • 2020-12-24 02:16

    Is what you posted your entire Activity?

    You also need to override onActivityResult, and pass the values to Session.getActiveSession().onActivityResult(...). Otherwise, the Session won't know that the user has authorized your app, and that's why you see the error (Session thinks that there's still a pending auth request, which is why you can't reauthorize for publish).

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