The following lines should be written in Manifest.xml
<activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/fb_app_id" />
If you get a token in this way, then you need to specify the fields of your own type as public_profile
list of these permissions can be found in the documentation. When receiving a user's token, you specify fields together with the request: public_profile
, user_friends
, etc.
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")); }
after you have logged in and received a token, you can make requests for obtaining, for example, a list of friends, in order to receive these same friends, you must have permission to receive them, here is an example of such a request:
public void getFriendsList() { AccessToken token = AccessToken.getCurrentAccessToken(); String userId = token.getUserId(); String path = "/friends"; new GraphRequest(token, userId + path, null, HttpMethod.GET, new GraphRequest.Callback() { public void onCompleted(GraphResponse response) { /* handle the result */ JSONArray data; ArrayList <FacebookUser> resultList = new ArrayList <FacebookUser> (); if (response.getError() == null) { try { //get data json object from response data = response.getJSONObject().getJSONArray("data"); //loop data for get list of friends for (int i = 0; i < data.length(); i++) { //get friend from array JSONObject object = (JSONObject) data.get(i); String name = object.getString("name"); long id = object.optLong("id", -1); //put user data to model and save to result list FacebookUser user = new FacebookUser(); user.setName(name); user.setId(id); resultList.add(user); } } catch (JSONException e) { e.printStackTrace(); } } Log.wtf(TAG, "Response error: " + response.getError()); Log.wtf(TAG, "Raw: " + response.getRawResponse()); } } ).executeAsync();
For a specific request, you need permissions: "user_friends", "read_custom_friendlists". Those. if you need some other info about the user, you need to request it when you receive the token.