0

I'm trying to get the name of a user from his user ID. on this link they say that we can do it from a simple HTTP request like that:

http://graph.facebook.com/4  

But it seems that this method is outdated because i cant get anything because:

"An access token is requir…o request this resource."

Anyway i tried using their documentation about the graph api on Android and i did it like that:

    GraphRequestAsyncTask request1 = new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/"+id1+"/name",
        null,
        HttpMethod.GET,

        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                /* handle the result */
            }
        }
    ).executeAsync();
    name1.setText(request1.toString());

But on the name1 TextView i get a message that tells me

{RequestAsyncTask:
    connection:null,requests:[{Request:
    accesToken:{AccessToken
        token:ACCESS_TOKEN_REMOVED permissions:
        [public_profile]},
        graphPath:/100017723671435/name, graphObject: null,
        httpMethod:Get,
        parameters:Bundle[{}]}]}

I don't really understand how to do, i have a bunch of facebook ID's on my database and i just want to get the name from the ID, to display it on the screen.

The current user of the app is not the profile which i want to get the infos!

EDIT: It seems i misunderstood the /*handle the result*/ commentary, i made this line like that:

name1.setText(response.toString());

But i have still an error on my TextView:

{Response: 
    responseCode:400,
    graphObject:null,
    error: { HttpStatus:400,
             errorCode: 2500,
             errorType: OAuthException,
             errorMessage: Unknown path components: /first_name }}

So it seems i don't use the Graph Paths properly. I'm still looking on the doc and on Google, if i find the answer i'll give the code!

DEEGROY
  • 17
  • 2
  • 1
    `first_name` is a field not an edge. https://developers.facebook.com/docs/graph-api/using-graph-api/#fields – CBroe Jun 01 '17 at 13:30

1 Answers1

0

Ensure that you are logged in with read permission. If you are logged in you can try with:

GraphRequest.Callback gCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            Log.d(TAG, "onCompleted: ");
            if (response != null && response.getJSONObject() != null && response.getJSONObject().has("first_name"))
            {
                try {
                    name1.setText(response.getJSONObject().getString("first_name"));
                } catch (JSONException e) {
                    Log.e(TAG, "onCompleted: ",e );
                }
            }
        }
    };
    new GraphRequest(AccessToken.getCurrentAccessToken(),"/me?fields=id,name,gender,email,first_name,last_name", null,HttpMethod.GET, gCallback).executeAsync();

If you have a problem with login, maybe this link can help you.

  • Okay thanks that works now! I don't really understand the graph doc so thanks for explaining me like that :D – DEEGROY Jun 02 '17 at 16:49