If your app is making enough calls to be considered for rate limiting by our system, we return an X-App-Usage HTTP header. [...] When any of these metrics exceed 100 the app will be rate limited.
我正在使用Facebook4J将我的应用程序连接到 Facebook API。但我找不到任何有关如何在 Facebook 调用后获取 X-App-Usage
HTTP header 的文档,以避免受到速率限制。我想使用此 header 动态地了解是否需要增加或减少每个 API 调用之间的时间。
所以,我的问题是:使用 Facebook4J,是否可以检查 Facebook 是否返回了 X-App-Usage
HTTP header 并获取它?怎么办?
最佳答案
facebook4j中有一个用于BatchRequests响应的getResponseHeader方法,参见Facebook4j code examples
您可以尝试 getResponseHeader("X-App-Usage")
// Executing "me" and "me/friends?limit=50" endpoints
BatchRequests<BatchRequest> batch = new BatchRequests<BatchRequest>();
batch.add(new BatchRequest(RequestMethod.GET, "me"));
batch.add(new BatchRequest(RequestMethod.GET, "me/friends?limit=50"));
List<BatchResponse> results = facebook.executeBatch(batch);
BatchResponse result1 = results.get(0);
BatchResponse result2 = results.get(1);
// You can get http status code or headers
int statusCode1 = result1.getStatusCode();
String contentType = result1.getResponseHeader("Content-Type");
// You can get body content via as****() method
String jsonString = result1.asString();
JSONObject jsonObject = result1.asJSONObject();
ResponseList<JSONObject> responseList = result2.asResponseList();
// You can map json to java object using DataObjectFactory#create****()
User user = DataObjectFactory.createUser(jsonString);
Friend friend1 = DataObjectFactory.createFriend(responseList.get(0).toString());
Friend friend2 = DataObjectFactory.createFriend(responseList.get(1).toString());
关于java - 如何使用 Facebook4J 获取 Facebook 速率限制 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795759/