java - Facebook Graph Java HttpsURLConnection 返回错误 400

标签 java facebook facebook-graph-api httpurlconnection inputstreamreader

我正在尝试通过 Web API 从 Facebook Graph 下载一些数据,直到现在一切正常,不知何故,这段代码给我一个 400 错误而不是 json 响应

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);

当它到达这一行时:

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

它会抛出一个 IOException 错误和这个信息:

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

在 chrome 中打开该 url 会返回页面,没有错误。

-- 编辑 --

如果有帮助,请检查 header 字段,这是它显示的内容:

{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]}

我会理解

Calls to this api have exceeded the rate limit

如果它在浏览器中也不起作用

最佳答案

Facebook 有速率限制 [1],这意味着一个用户不能向 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.

编辑:

613号错误指的是另一条规则:

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.

这意味着来自浏览器的单个调用可能不会被此规则捕获,但如果您的代码发送一堆请求,它将失败。

更多信息:

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

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

关于java - Facebook Graph Java HttpsURLConnection 返回错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44806264/

相关文章:

php - 使用 FQL 的随机好友

ios - 为什么 IOS-API 在我自己的页面上张贴 facebook 页面上的照片?

javascript - Facebook Canvas,无法在移动设备上从 Facebook 登录

Java - 在另一个实例中添加抽象类的实例

java - 如何使用jpcap捕获数据包?

php - 如何设置 facebook 流 api

java - 适用于 Android 的 Facebook key 哈希

java - 圆角mapbox mapView

java - 2D 圆低速碰撞导致重叠

android - Facebook API 是否允许发送和接受好友请求?