How to get facebook user access token in android?

Junaid picture Junaid · May 2, 2013 · Viewed 32.5k times · Source

I am using restfb API to access my friend's photos in Java. And to access these photos, I generate access code manually using Graph API explorer and pass this as a parameter to the restfb API call.

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.

I have seen these solutions (solution-1 & solution-2) on stackoverflow but I am still not getting where to start?

Update-1::

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.

And just in case if you don't know where to start, follow step-6 to create a new app from scratch.

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() + "!");
                                    }
                                }
                            });
                }
            }
        });

    }

    }

Answer

5agado picture 5agado · May 2, 2013

Using the Facebook SDK the better way to manage the authentication of the user is by means of the Session class. When you have a valid instance of the Session class you just have to call the getAccessToken() on it in order to obtain the String representing the access token.

Session session = Session.getActiveSession();
if (session != null && session.getState().isOpened()){
     Log.i("sessionToken", session.getAccessToken());
     Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
}