title: Android - Facebook SDK 3 - How to login programmatically without LoginButton
body: <p>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 LoginButton to log the user in.</p>
  
  <p>I don't want that. All I want to do is click my share button in the actionbar and share it to facebook. Therefore I want to login programmatically, I tried to emulate what the LoginButton does but no success so far. I get</p>
  
  <blockquote>
    <p>12-06 15:34:33.180: E/AndroidRuntime(19493):
    java.lang.UnsupportedOperationException: Session: an attempt was made
    to reauthorize a session that has a pending request.</p>
  </blockquote>
  
  <p>public class MainActivity extends FacebookActivity {</p>
  
  <pre><code>@SuppressWarnings("serial")
  private static final List&lt;String&gt; PERMISSIONS = new ArrayList&lt;String&gt;() {
      {
          add("publish_actions");
      }
  };
  private final int REAUTHORIZE_ACTIVITY = 3;
  private Button postStatusUpdateButton;
  private PendingAction pendingAction = PendingAction.NONE;
  
  private enum PendingAction {
      NONE, POST_PHOTO, POST_STATUS_UPDATE
  }
  
  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton);
      postStatusUpdateButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
              Log.d("MainActivity", "onClick");
              onClickPostStatusUpdate();
          }
  
      });
  
  }
  
  @Override
  protected void onSessionStateChange(SessionState state, Exception exception) {
      super.onSessionStateChange(state, exception);
  }
  
  private interface GraphObjectWithId extends GraphObject {
      String getId();
  }
  
  private void showPublishResult(String message, GraphObject result, FacebookRequestError error) {
      String title = null;
      String alertMessage = null;
      if (error == null) {
          title = getString(R.string.success);
          String id = result.cast(GraphObjectWithId.class).getId();
          alertMessage = getString(R.string.successfully_posted_post, message, id);
      } else {
          title = getString(R.string.error);
          alertMessage = error.getErrorMessage();
      }
  
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(title).setMessage(alertMessage).setPositiveButton(getString(R.string.ok), null);
      builder.show();
  }
  
  private void onClickPostStatusUpdate() {
      Log.d("MainActivity", "onClickPostStatusUpdate");
      performPublish(PendingAction.POST_STATUS_UPDATE);
  }
  
  private boolean hasPublishPermission() {
      Session session = Session.getActiveSession();
      return session != null &amp;&amp; session.getPermissions().contains("publish_actions");
  }
  
  private void performPublish(PendingAction action) {
      Log.d("MainActivity", "peformPublish");
  
      Session session = Session.getActiveSession();
  
      if (session == null) {
          session = new Session.Builder(this).setApplicationId("xxx").build();
          Session.setActiveSession(session);
      }
  
      if (!session.isOpened()) {
          Session.OpenRequest openRequest = new Session.OpenRequest(this);
          openRequest.setPermissions(PERMISSIONS);
          openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
          session.openForPublish(openRequest);
      }
  
      if (session != null) {
          // postStatusUpdate();
          pendingAction = action;
          if (hasPublishPermission()) {
              // We can do the action right away.
              handlePendingAction();
              // postStatusUpdate();
          } else {
              // We need to reauthorize, then complete the action when we get
              // called back.
              Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS)
                      .setRequestCode(REAUTHORIZE_ACTIVITY).setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
              session.reauthorizeForPublish(reauthRequest);
          }
      }
  }
  
  @SuppressWarnings("incomplete-switch")
  private void handlePendingAction() {
      PendingAction previouslyPendingAction = pendingAction;
      // These actions may re-set pendingAction if they are still pending, but
      // we assume they
      // will succeed.
      pendingAction = PendingAction.NONE;
  
      switch (previouslyPendingAction) {
      case POST_STATUS_UPDATE:
          postStatusUpdate();
          break;
      }
  }
  
  private void postStatusUpdate() {
      // if (user != null &amp;&amp; hasPublishPermission()) {
      if (hasPublishPermission()) {
          // final String message = getString(R.string.status_update,
          // user.getFirstName(), (new Date().toString()));
          final String message = "kks uz nemam nervy";
          Request request = Request.newStatusUpdateRequest(Session.getActiveSession(), message,
                  new Request.Callback() {
                      @Override
                      public void onCompleted(Response response) {
                          showPublishResult(message, response.getGraphObject(), response.getError());
                      }
                  });
          Request.executeBatchAsync(request);
      } else {
          pendingAction = PendingAction.POST_STATUS_UPDATE;
      }
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Log.d("MainActivity", "onActivityResult");
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Well, this is still the HelloFacebookSample project which im trying to bend the right way. The only thing I've played with is the performPublish method, stuff with creating the session.</p>
  
  <p>Hope there is an easier way tho!
  P.S.: <strong>I am using Facebook SDK 3</strong></p>