Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph Java HttpsURLConnection returns error 400

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

like image 793
DomeWTF Avatar asked Apr 25 '26 13:04

DomeWTF


1 Answers

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?

like image 198
mico Avatar answered Apr 28 '26 01:04

mico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!