Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    login = (Button) findViewById(R.id.login);
    accessToken = (TextView) findViewById(R.id.accessToken);

    login.setOnClickListener(this);

}

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

}

}

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.

Thanks.

like image 379
Anupam Avatar asked Feb 21 '13 07:02

Anupam


People also ask

How can I get Facebook token in android?

To get the Client Token for an app, do the following: Sign into your developer account. On the Apps page, select an app to open the dashboard for that app. On the Dashboard, navigate to Settings > Advanced > Security > Client token.

How do I get my Exchange token on Facebook?

At a high level, you obtain a long-lived token for the client by: Using a valid, long-lived access token, your server sends a request to get a code from Facebook. Facebook sends a code back to your server and you securely send this code to the client.

How do I get a long-lived access token on Facebook?

To get a long-lived User access token, send a GET request to the /oauth/access_token endpoint. Replace APP-ID , APP-SECRET , and SHORT-LIVED-USER-ACCESS-TOKEN with your information. This token is valid for 60 days.


2 Answers

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()"

like image 88
Erick Reátegui Diaz Avatar answered Sep 22 '22 15:09

Erick Reátegui Diaz


Note: Session class is removed in new Facebook sdk

After Facebook SDK version 4.X, you should use following:

AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
    Toast.makeText(getActivity(), token, Toast.LENGTH_LONG).show();
}

Change log reference is here.

Migrate reference from Sdk 3.x to 4.x

like image 27
uniruddh Avatar answered Sep 25 '22 15:09

uniruddh