skip to Main Content

I’m trying to download some data from Facebook Graph via the web api and everything worked fine until now, somehow, this code is giving me a 400 error instead of the json response

URL url = new URL(link);
URLConnection urlConn = url.openConnection();

urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.setRequestProperty("Accept-Charset", "UTF-8");
urlConn.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");

InputStreamReader in = new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(in);

When it reaches this line:

InputStreamReader in = new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8);

It throws an IOException error with this message:

Server returned HTTP response code: 400 for URL: https://graph.facebook.com/v2.9/sear

Opening that url in chrome returns the page just fine without errors.

EDIT

If it helps, checking the header fields this is what it shows:

{Transfer-Encoding=[chunked], x-fb-trace-id=[G1Wlv7GX9w8],
null=[HTTP/1.1 400 Bad Request], Access-Control-Allow-Origin=[*],
WWW-Authenticate=[OAuth “Facebook Platform” “invalid_request” “(#613)
Calls to this api have exceeded the rate limit.”],
Connection=[keep-alive], x-fb-rev=[3122154], Pragma=[no-cache],
Date=[Wed, 28 Jun 2017 15:26:45 GMT], Cache-Control=[no-store],
Vary=[Accept-Encoding], Expires=[Sat, 01 Jan 2000 00:00:00 GMT],
X-FB-Debug=[REMOVED],
facebook-api-version=[v2.9], Content-Type=[application/json;
charset=UTF-8]}

I would understand the

Calls to this api have exceeded the rate limit

If it wasn’t working in the browser either

2

Answers


  1. Facebook has a rate limit [1], which means one user cannot send too many calls to the API:

    Your app can make 200 calls per hour per user in aggregate. As an example, if your app has 100 users, this means that your app can make 20,000 calls. This isn’t a per-user limit, so one user could make 19,000 of those calls and another could make 1,000. This limit is calculated based on the number of calls made in the previous hour.

    Edit:

    Number 613 error refers to another rule:

    However, the error code: 613 – FQL_EC_RATE_LIMIT_EXCEEDED – Calls to stream have exceeded the rate of 100 calls per 600 seconds

    specifies that you can averagely perform up to 1 call per 6 seconds.

    This means single call from browser may not be catched by this rule, but if your code sends a bunch of requests, it will fail.

    More info:

    [1] https://developers.facebook.com/docs/graph-api/advanced/rate-limiting

    [2] Conflicting limits about maximum number of calls to Facebook API?

    Login or Signup to reply.
  2. I tried your code and it works perfectly as long as the URL is correct.

    For example when I removed the access_token from the URL query parameters it gave me the error HTTP 400.

    Server returned HTTP response code: 400 for URL: https://graph.facebook.com/v2.9/me?&debug=all&fields=id,name&format=json&method=get&pretty=0

    If I add the &access_token= back, it works like a charm for me.

    Hoping you can see if anything like this happening on your side.

    IF you could share the absolute URL obscuring your access token, I can debug further.

    –EDIT–

    Please use this code adjustment to print the reason of the HTTP 400 so you might figure out the root cause.

    URL url = new URL(link);
        HttpURLConnection  urlConn = (HttpURLConnection) url.openConnection();
    
        urlConn.setRequestProperty("Content-Type", "application/json");
        urlConn.setRequestProperty("Accept-Charset", "UTF-8");
        urlConn.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
    
        //InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
        InputStreamReader in = null;
        if (urlConn.getResponseCode() < urlConn.HTTP_BAD_REQUEST) {
            in = new InputStreamReader(urlConn.getInputStream());
        } else {
             /* error from server */
            in = new InputStreamReader(urlConn.getErrorStream());
        }
    
    
        BufferedReader reader = new BufferedReader(in);
        System.out.println(reader.readLine());
    

    Thanks!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search