title: Android Facebook SDK - "Share Dialog with Login"
body: <p>I'm trying to implement a 'Share Dialog' Facebook button in Android. It differs from the examples, because there is no separate login button, so the flow is all within the Share button. On my HTC Desire it works: The user clicks the button. If they aren't logged in, the Facebook native app starts and they get prompts to authorize the app for basic and publish scopes (one after the other as the docs require), then they can use the native share dialog to post to Facebook. On various other phones, I just get the infamous:</p>
  
  <p>"An error occurred. Please try again later."</p>
  
  <p>I've read several threads on this on StackOverflow and tried so many things now but I've run out of ideas, and this needs to work. What really surprises me is that there is no demo or example code online or anywhere on how to do this - surely this is what 90% of developers want to use the Android Facebook SDK for? Most apps won't have a separate Facebook login button just so sharing works, will they?</p>
  
  <p>Here is my code. Any help would be really useful!</p>
  
  <p><strong>In Application.onCreate():</strong></p>
  
  <pre><code>    String facebook_id = getApplicationContext().getString(R.string.facebook_id);
      facebookSession = Session.getActiveSession();
      Log.d("Application", "** facebookSession = "+facebookSession);
  
      if (facebookSession == null || facebookSession.getState().isClosed()) {
          facebookSession = new Session.Builder(this).setApplicationId(facebook_id).build();
          Log.d("Application", "** Built new session: "+facebookSession);
          Session.setActiveSession(facebookSession);
      }
  </code></pre>
  
  <p><strong>In my Fragment's Facebook share button listener:</strong></p>
  
  <pre><code>protected void tryFacebookShare() {
  
      StatusCallback callback = new StatusCallback() {
          public void call(Session session, SessionState state, Exception exception) {
              if (exception != null) {
                  Log.d("facebook", "Error in Facebook login: "+exception.getMessage());
                  Application.refreshSession();
              }
              Log.d("facebook", "Session StatusCallback returned - state: "+state.toString()+" session: "+session.toString()+" token: "+session.getAccessToken());
  
              if(state == SessionState.OPENED || state == SessionState.OPENED_TOKEN_UPDATED) {
                  if(session.getPermissions().contains("publish_actions")) {
                      Log.d("facebook", "We have permissions, publishing");
                      PollFragment.this.publishPollStory();
                  } else {
                      Log.d("facebook", "Requesting publish permissions");
                      Session.NewPermissionsRequest newPermissionsRequest = new Session
                              .NewPermissionsRequest(PollFragment.this, Arrays.asList("publish_actions"));
                      session.requestNewPublishPermissions(newPermissionsRequest);
                  }
              }
          }
      };
  
      Session session = Application.getFacebookSession();
      if(!session.isOpened() &amp;&amp; !session.isClosed()) {
          Log.d("facebook", "Opening new session");
          session.openForRead(new Session.OpenRequest(getActivity()).setPermissions(Arrays.asList("basic_info")).setCallback(callback));
      } else {
          Log.d("facebook", "Opening active session...");
          Session.openActiveSession(getActivity(), this, true, callback);
      }
      }
  </code></pre>
  
  <p>publishPollStory() opens the share dialog, and seems to work fine. The problems occur before it gets this far: usually I get as far as the StatusCallback reporting it's in SessionState.OPENING, then the WebView gives me the "Error has occurred" and I'm stuck. I don't get onActivityResult() being called either.</p>
  
  <p>Any ideas? Thanks in advance, kind StackOverflow users! :)</p>