DsCpp
DsCpp

Reputation: 2489

Get the URL of a facebook account using the new GRAPH API in android

Using the new graph api we manage to get the profile picture of are users(using the login with facebook button).
here's a snippet.

   GraphRequest request = GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Application code
                        try {
                            Log.i("Response", response.toString());
                            Person person = new Person();
                            person.setEmail(response.getJSONObject().getString("email"));
                            person.setName(response.getJSONObject().getString("first_name") + " " +
                                    response.getJSONObject().getString("last_name"));
                            person.setDevice_token(FirebaseInstanceId.getInstance().getToken());
                            if (response.getJSONObject().has("picture")) {
                                person.setPersonProfileImageURL(response.getJSONObject().getJSONObject("picture").getJSONObject("data").getString("url"));

                            }
                            person.setmPersonUid(uid);

                            mDatabase.setValue(person).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if (task.isSuccessful()) {
                                        Log.i(" mDatabase.setValue", "task.isSuccessful");
                                    }

                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,email,first_name,last_name,picture.type(large)");

Is there a way to get a link to their facebook profile as well?? (i.e we want to show a link to the facebook profile in our app)
We thought of maybe concatenating the id with facebook.com/{id} but that didn't do it.
Thanks

Upvotes: 1

Views: 797

Answers (1)

Sandeep Parish
Sandeep Parish

Reputation: 2238

You need to get permission to access user_link and then try this code.

for setting permission use this or add in you are using already.

loginButton.setReadPermissions("user_link");

For getting profile url from graph api.

GraphRequest request = GraphRequest.newMeRequest(
  accessToken,
  new GraphRequest.GraphJSONObjectCallback() {
    @Override
    public void onCompleted(JSONObject object, GraphResponse response) {
      // do your stuff
    }
});

Bundle parameters = new Bundle();
parameters.putString("fields", "link");
request.setParameters(parameters);
request.executeAsync();

it works for me hope also works for you.

comment below if it works for you.

Upvotes: 1

Related Questions