Anupam Anupam - 1 year ago 179
Android Question

Android: Facebook get "User Access Token", on successful login

I have been doing some login project via Facebook latest SDK i.e. 3.0. I'm getting a hard time in getting user access token. I have searched on the internet and all, maximum results were using the old SDK. Here is some bits of code which I have taken from Facebook Android SDK Tutorial:

public class LoginActivity extends Activity implements OnClickListener {

Button login;
TextView accessToken;

protected void onCreate(Bundle savedInstanceState) {

login = (Button) findViewById(;
accessToken = (TextView) findViewById(;



public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);

public void onClick(View v) {
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {

// callback when session changes state
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {

// make request to the /me API
new Request.GraphUserCallback() {

// callback after Graph API response with user
// object
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(;
welcome.setText("Hello "
+ user.getName() + "!");



Login was successful and I can see Username in the app, as suggested by the Facbeook tutorial.

I have tried old methods, but those all are now deprecated. Please guide me in getting User Access Token. Help will be appreciated.


Answer Source

In the method onResume() add the following code (in this case i used Toast.makeText to see the token access after login):

    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        Toast.makeText(getActivity(), session.getAccessToken(), Toast.LENGTH_LONG).show();

I used getActivity because it is in a Fragment in my case, if you have your login button in an Activity use "this" instead of "getActivity()"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download