Daniel Viglione
Daniel Viglione

Reputation: 9407

Get authentication token and user information from Facebook

I am using Facebook Login for Android. It works. I can log in through the emulator. However, I need to send back information to my Rails Rest API. Upon facebook callback success, I want to send back the access token, the provider ("facebook"), the uid, facebook user name, facebook user email and the facebook image icon.

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  @Override
  public void onSuccess(LoginResult loginResult) {
    Log.i("AUTHENTICATION TOKEN", String.valueOf(loginResult.getAccessToken()));
  }  

Logcat shows the following:

I/AUTHENTICATION TOKEN: {AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, email]}

How do I get the information I need onSuccess?

Upvotes: 1

Views: 797

Answers (2)

ZeroOne
ZeroOne

Reputation: 9117

try this

Log.i("AUTHENTICATION TOKEN", AccessToken.getCurrentAccessToken().getToken());

Upvotes: 0

ashkhn
ashkhn

Reputation: 1628

Once you have the access token obtained using LoginResult.getAccessToken(),

You need to call the Facebook Graph api using the access token received. From the SDK Docs,

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // Application code
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync()

The JSONObject object will contain the required user information provided the user has granted all the necessary permissions to your application

The list of fields available for user can be seen here

Upvotes: 1

Related Questions