3

I do not understand why this query does not work in android and "Graph Api Explorer" if it

111891332274505?fields=albums.fields(id,name,description,updated_time,created_time,cover_photo)

Error Code

Error code: 2500 Error mensaje: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: Syntax error "Expected end of string instead of "?"." at character 6: photos?access_token=XXXXXXXXXXXX}

Code

     public void getAlbumFacebook () {
     Request request = new Request(session, "/111891332274505?fields=albums.fields(id,name,description,updated_time,created_time,cover_photo)", null, HttpMethod.GET, new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            GraphObject graphObject = response.getGraphObject();
            FacebookRequestError error = response.getError();

             if (error != null) {
                 DebugLog.log("Error code: " + error.getErrorCode() + " Error mensaje: " + error.toString());

            }

            if (graphObject != null) {
                JSONArray jsonArray = (JSONArray) graphObject.getProperty("data");
                DebugLog.log("JSON Facebook "+ jsonArray.toString());
            } else {
                 DebugLog.log("Es null");

            }
        }
    });

    Request.executeBatchAsync(request);
 }

SOLUTION

I've already been able to solve the problem I can deduce is that the "Request" class can not consult with inserted fields.

In the end what I have done is to build the url and request data through the url.

Build URL

 String id = "111891332274505";
 String url = "https://graph.facebook.com/" + id + "/albums?access_token=" + session.getAccessToken() + "&fields=id,name,description,updated_time";

Code request

     public JSONObject getRequestFromUrl (String url) {
     try {
         httpClient = new DefaultHttpClient();
         HttpGet get = new HttpGet(url);

         HttpResponse httpResponse = httpClient.execute(get);
         HttpEntity responseEntity = httpResponse.getEntity();
         is = responseEntity.getContent();
         DebugLog.log("Estoy aqui");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");             
        }

        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
 }
joselufo
  • 3,393
  • 3
  • 23
  • 37
  • You need to include your access token for the user. – hichris123 Nov 28 '13 at 19:36
  • Thanks @hichris123 , but how to include the access token for the user? – joselufo Nov 28 '13 at 21:01
  • Have you gone through the OAuth authentication? – hichris123 Nov 28 '13 at 21:07
  • Yes, I'm logged in to facebook and session object is open also if I change the query to this "/111891332274505/albums" works but when I put fields and gives me that error. – joselufo Nov 28 '13 at 21:12
  • Oh, look at this. `Syntax error "Expected end of string instead of "?"." at character 6: photos?access_token=XXXXXXXXXXXX}` Let me look at the API reference. – hichris123 Nov 28 '13 at 21:23
  • Anyone could help me with my question? :( http://stackoverflow.com/questions/30430383/how-can-i-get-tagged-places-of-my-friends – Vitor May 25 '15 at 16:56

3 Answers3

8

There should be an ampersand sign & just before the access_token parameter. But right now, a ? is being appended before that parameter. The problem is the part of the code that is appending the ? instead of & in the URL.

Rahil Arora
  • 3,644
  • 3
  • 26
  • 43
0

By RestFB for java you can make use of Parameter class.

Parameter.with("fields", "albums.fields(id,name,description,updated_time,created_time,cover_photo") in your example. (API may change)

Robin Loxley
  • 824
  • 8
  • 10
-1

I had the same problem but with GraphRequest class, so I'll write for it.

GraphRequest will make URL like this

...facebook.com/111891332274505?fields=albums...?access_token=...

instead of

...facebook.com/111891332274505?fields=albums...&access_token=...

as it should be.

Solution:

GraphRequest request =  GraphRequest.newGraphPathRequest(access_token, "111891332274505",...);

Bundle parameters = new Bundle();

parameters.putString("fields", "albums...");

request.setParameters(parameters);

request.executeAsync();
Dark
  • 864
  • 9
  • 17