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
Facebook has a rate limit [1], which means one user cannot send too many calls to the API:
Edit:
Number 613 error refers to another rule:
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?
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.
Thanks!