I am using Firebase Authentication for my Android app. Users have the ability to login with multiple providers (Google, Facebook, Twitter).
After a successful login,
For Facebook :
To get facebook accessToken from firebase is very simple. I was using firebase auth UI. After authentication with facebook you will get basic information from firebase user object like display name, email,provider details. But if you want more information like gender, birthday facebook Graph API is the solution. Once user authenticated with the facebook you can get access token like this.
AccessToken.getCurrentAccessToken() But sometimes it will give you NULL value instead of valid access token. Make sure that you have initialized facebook SDK before that.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(this);
}
} After initialization use graphAPI
if(AccessToken.getCurrentAccessToken()!=null) {
System.out.println(AccessToken.getCurrentAccessToken().getToken());
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
String email = object.getString("email");
String gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
else
{
System.out.println("Access Token NULL");
}
Happy Coding :)