Domanda

I am new to the facebook SDK and I am encountering this issue: After login I have then Session object, the I want to execute some graph API request. Here is my code:

Request request = new Request(session, "me?fields=email,installed,first_name,last_name");
Response response = request.executeAndWait();
GraphObject obj = response.getGraphObject();
JSONObject json = obj.getInnerJSONObject();

Here is the Request object (toString()):

{Request:  session: {Session state:OPENED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, email, contact_email, user_hometown]}, appId:MY_APP_ID}, graphPath: me?fields=email,installed,first_name,last_name, graphObject: null, restMethod: null, httpMethod: GET, parameters: Bundle[{access_token=ACCESS_TOKEN_STRING_OF_SESSION, format=json, sdk=android}]}

And here is the Response object (toString()):

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: An active access token must be used to query information about the current user.}, isFromCache:false}

When I am executing the request in the browser like this:

https://graph.facebook.com/v2.0/me?access_token=ACCESS_TOKEN_STRING_OF_SESSION

I am getting a valid json response.

So I am getting this error as seen in the response, why is it? every thing seems ok with the Request object.

Thanks

È stato utile?

Soluzione

Ok, here is the solution that worked for me:

Bundle bundle = new Bundle();
bundle.putString("fields", "email,installed,first_name,last_name");
Request request = new Request(session, "me", bundle, HttpMethod.GET);
Response response = request.executeAndWait();
GraphObject obj = response.getGraphObject();
JSONObject json = obj.getInnerJSONObject();

I didn't find any documentation about this sort of API calls.

Altri suggerimenti

After trying @vileo solution with a bundle parameter:

Bundle params = new Bundle();
params.putString("fields", "id,name,icon,administrator");
params.putString("icon_size", "34");
// Put the access token in it
params.putString("access_token", Session.getActiveSession().getAccessToken());

And remove all following fields from this:

me/groups?fields=id,name,icon,administrator&icon_size=34

To this:

/me/groups

To do the request with this:

Bundle params = new Bundle();
params.putString("fields", "id,name,icon,administrator");
params.putString("icon_size", "34");
new Request(
    Session.getActiveSession(),
    "/me/groups",
    params,
    HttpMethod.GET,
    new Request.Callback() {
        public void onCompleted(Response response) {
            if (response.getError() != null) {
                LogUtils.LOGE(TAG, "@ Request user_group failed: " + response.getError().getErrorMessage());

            } else {

                GraphObject go = response.getGraphObject();
                UserGroup userGroup =
                        new Gson().fromJson(go.getInnerJSONObject().toString(), UserGroup.class);

                LogUtils.LOGD(TAG, "Group string::: " + go.getInnerJSONObject().toString());
                if (userGroup != null) {
                    PrefUtils.setFbUserGroup(activity, userGroup);
                }
            }
        }
    }
).executeAsync();    

Finally I got the correct response...

Group string::: {"data":[{"id":"258966464271933","icon":"https:\/\/fbstatic-a.akamaihd.net\/rsrc.php\/v2\/yH\/r\/k5fKtX9s4PO.png","name":"[咪04] 人才許願池(徵才\/求職\/接案\/發案\/合作)"},{"id":"197223143437","icon":"https:\/\/fbstatic-a.akamaihd.net\/rsrc.php\/v2\/y5\/r\/La_vzova_d2.png","name":"Python Taiwan"},{"id":"262800543746083","icon":"https:\/\/fbstatic-a.akamaihd.net\/rsrc.php\/v2\/yf\/r\/tDvnAIzL8Ft.png","name":"node.js台灣"},...}}

I'm curious why FB not mention this to keep us guessing around..

Vlio20, I was having this same problem, so thank you for posting your solution; it worked. I decided to post this because the general format of the new Facebook queries wasn't explained.

LoginManager.getInstance().registerCallback(callbackManager,
    new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        /* make the API calls */
                        Bundle myBundle = new Bundle();
                        myBundle.putString("fields" , "id,name,picture,friends{id,name,picture}");
                        new GraphRequest(
                                loginResult.getAccessToken(),
                                "/me",
                                myBundle,
                                HttpMethod.GET,
                                new GraphRequest.Callback() {
                                    public void onCompleted(GraphResponse response) {
                                        /* handle the result */
                                        if (response.getError() != null) {
                                            // handle error
                                            System.out.println("ERROR");
                                        } else {
                                            System.out.println("Success");
                                        }
                                    }
                                }
                        ).executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        // App code
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                    }
          });

This gets the user's public Facebook profile and it gets the user's in-app friend list.

If you need to go one path level deeper, you just use the local name of the path. For example, if your base path is "/me", you can go one level deeper in the path by typing "friends" into the bundle string along with any properties of those friends that you need inside of brackets "{id, name, picture}".

This way you don't need two calls to get the information from Facebook :)

Appears that your request is missing an access token, and you must have a user access token to read information about people.

Get started with Facebook Login on Android:

https://developers.facebook.com/docs/android/login-with-facebook/v2.0

Another good way to debug this / similar issues is using curl in a shell - you can get an access token to use with curl here:

https://developers.facebook.com/tools/access_token/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top