title: How to get facebook user access token in android?
body: <p>I am using <a href="http://restfb.com/" rel="nofollow noreferrer">restfb API</a> to access my friend's photos in Java. And to access these photos, I generate access code manually using <a href="https://developers.facebook.com/tools/explorer" rel="nofollow noreferrer">Graph API explorer</a> and pass this as a parameter to the restfb API call. </p>
  
  <p>But now I want to generate this access token through code (programmatically). I have seen fb android samples, particularly hackbook. I don't see any access code being generated which I can use for my own application. Do I need to create a new app and get some secret etc? Any suggestion will be appreciated. </p>
  
  <p>I have seen these solutions (<a href="https://stackoverflow.com/questions/14991377/facebook-how-to-get-permanent-user-access-token">solution-1</a> &amp; <a href="https://stackoverflow.com/questions/14996868/android-facebook-get-user-access-token-on-successful-login">solution-2</a>) on stackoverflow but I am still not getting where to start?</p>
  
  <p><strong>Update-1</strong>:: </p>
  
  <p>I am using following code to login and getting access token for logged in user. But the problem is that it only works for the account with which I had created an app on facebook to generate an app_id. It does not get a call back for other accounts. Any suggestion please.</p>
  
  <p>And just in case if you don't know where to start, follow <a href="https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/" rel="nofollow noreferrer">step-6</a> to create a new app from scratch.</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
      Button login;
      TextView accessToken;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          login = (Button) findViewById(R.id.login);
          accessToken = (TextView) findViewById(R.id.accessToken);
  
          login.setOnClickListener(this);
          // start Facebook Login
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
      }
  
      @Override
      public void onResume()
      {
           Session session = Session.getActiveSession();
           if(session != null)
              if (session.isOpened()) {
                  //Toast.makeText(this, session.getAccessToken(), Toast.LENGTH_LONG).show();
                  accessToken = (TextView) findViewById(R.id.accessToken);
                  accessToken.setText(session.getAccessToken());
                  System.out.println("----------------------" + session.getAccessToken() + "---------------------");
  
              }
          super.onResume();
      }
  
      @Override
      public void onClick(View v) {
          // 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.executeMeRequestAsync(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() + "!");
                                      }
                                  }
                              });
                  }
              }
          });
  
      }
  
      }
  </code></pre>