N
Initiate Facebook SDK and replicate callback :public void initFacebookSdk() {
FacebookSdk.sdkInitialize(activity.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
Log.d(TAG, "Facebook getApplicationId: " + loginResult.getAccessToken().getApplicationId());
Log.d(TAG, "Facebook getToken: " + loginResult.getAccessToken().getToken());
Log.d(TAG, "Facebook getUserId: " + loginResult.getAccessToken().getUserId());
Log.d(TAG, "Facebook getExpires: " + loginResult.getAccessToken().getExpires());
Log.d(TAG, "Facebook getLastRefresh: " + loginResult.getAccessToken().getLastRefresh());
}
@Override
public void onCancel() {
Toast.makeText(activity, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(activity, exception.getMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, exception.getMessage());
}
});
}
Then you're making a call to activate the logic.public void callLoginActivity() {
LoginManager loginManager = LoginManager.getInstance();
loginManager.setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK);
loginManager.logInWithReadPermissions(
activity,
Arrays.asList("public_profile", "user_friends", "read_custom_friendlists"));
}
As a result, you're in method onSuccess(LoginResult loginResult) return the data, after which you can be able to make other user data requests with the data received token♪After you get the token, you can ask him:/**
Get current user access token by Facebook
@return - instance of current {@link com.facebook.AccessToken}
/
public AccessToken getAccessToken() {
if (AccessToken.getCurrentAccessToken() == null) {
System.out.println("not logged in yet");
} else {
System.out.println("Logged in");
}
return AccessToken.getCurrentAccessToken();
}
Next, if you want any Facebook user profiles, you're making a request. Graph ApiHere's an example of a request that will give you data such as a name,id,avatar:/*
GraphApi request for get device user info
from Facebook as: id, name, avatar
*/
public void getMeInfo() {
AccessToken token = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newGraphPathRequest(
token,
PATH_ME,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject object; //raw response json object
JSONObject pictureData; //json object which contains picture data
try {
//check is response is not empty
if (response.getError() == null){
//parse json
object = new JSONObject(response.getRawResponse().toString());
pictureData = object.getJSONObject("picture").getJSONObject("data");
long id = Long.parseLong(object.optString("id");
String name = object.getString("name");
String url = pictureData.optString("url");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
//request params
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture.type(large)");
request.setParameters(parameters);
request.executeAsync();
}
All other requests GraphApi They're doing this.
Reference https://developers.facebook.com/docs/graph-api/reference and reference https://developers.facebook.com/tools/explorer in it in which you can challenge requests and you can generode the code of this request for your application, that is, you'll just bury it to your app and use it, it's very convenient.