Facebook Graph Java HttpsURLConnection returns 400 error

I am trying to load some data from Facebook graph via web api and everything works fine so far, anyway this code gives me 400 error instead of 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 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 with no errors.

- CHANGE -

If it helps by 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; encoding = UTF-8]}

I would understand

Calls to this api have exceeded the rate limit

If it doesn't work in the browser,

+3


source to share


2 answers


Facebook has a rate limit [1], which means that one user cannot make too many API calls:

Your application can make 200 calls per hour per user in total. For example, if your application has 100 users, this means that your application can make 20,000 calls. This is not a user limit, so one user can make 19,000 of these calls and another can make 1000. This limit is calculated based on the number of calls made in the previous hour.

Edit:

Error number 613 refers to a different rule:

However, the error code is: 613 - FQL_EC_RATE_LIMIT_EXCEEDED - Calls to the thread have exceeded 100 calls in 600 seconds

indicates that you can, on average, make up to 1 call in 6 seconds.



This means that a single call from the browser cannot be caught by this rule, but if your code is making a bunch of requests it will fail.

Additional Information:

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

[2] Conflicting limits on the maximum number of Facebook API calls?

+2


source


I tried your code and it works fine as long as the url is correct.

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

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 & access_token = back it works like a charm for me.

Hoping that you see something like this happening on your side.



IF you can share the absolute url hiding your access token, I can debug further.

- EDIT -

Please use this code setting to print the HTTP 400 reason so you can understand 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());

      

Thank!

0


source







All Articles