/** * Coordinates the process of executing the API request GET/POST and * processing the response we receive from the endpoint. * * @param endpoint Facebook Graph API endpoint. * @param parameters Arbitrary number of parameters to send along to Facebook as part * of the API call. * @return The JSON returned by Facebook for the API call. * @throws FacebookException If an error occurs while making the Facebook API POST or * processing the response. */ protected String makeRequest(String endpoint, Parameter... parameters) { return makeRequest(endpoint, false, false, false, null, parameters); }
/** * Coordinates the process of executing the API request GET/POST and processing the response we receive from the * endpoint. * * @param endpoint * Facebook Graph API endpoint. * @param parameters * Arbitrary number of parameters to send along to Facebook as part of the API call. * @return The JSON returned by Facebook for the API call. * @throws FacebookException * If an error occurs while making the Facebook API POST or processing the response. */ protected String makeRequest(String endpoint, Parameter... parameters) { return makeRequest(endpoint, false, false, null, parameters); }
/** * @see com.restfb.FacebookClient#deleteObject(String) */ @Override public boolean deleteObject(String object) { verifyParameterPresence("object", object); return "true".equals(makeRequest(object, false, true, true, null)); }
/** * @see com.restfb.FacebookClient#publish(String, Class, * java.io.InputStream, com.restfb.Parameter[]) */ @Override public <T> T publish(String connection, Class<T> objectType, InputStream binaryAttachment, Parameter... parameters) { verifyParameterPresence("connection", connection); return jsonMapper.toJavaObject(makeRequest(connection, false, true, false, binaryAttachment, parameters), objectType); }
/** * @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment, * com.restfb.Parameter[]) */ @Override public <T> T publish(String connection, Class<T> objectType, List<BinaryAttachment> binaryAttachments, Parameter... parameters) { verifyParameterPresence("connection", connection); return jsonMapper.toJavaObject(makeRequest(connection, true, false, binaryAttachments, parameters), objectType); }
/** * @see com.restfb.FacebookClient#executeBatch(java.util.List, java.util.List) */ @Override public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments) { verifyParameterPresence("binaryAttachments", binaryAttachments); if (batchRequests == null || batchRequests.isEmpty()) { throw new IllegalArgumentException("You must specify at least one batch request."); } return jsonMapper.toJavaList( makeRequest("", true, false, binaryAttachments, Parameter.with("batch", jsonMapper.toJson(batchRequests, true))), BatchResponse.class); }
/** * @see com.restfb.FacebookClient#fetchObject(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("object", object); verifyParameterPresence("objectType", objectType); return jsonMapper.toJavaObject(makeRequest(object, parameters), objectType); }
/** * @see com.restfb.FacebookClient#fetchObject(String, * Class, com.restfb.Parameter[]) */ @Override public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("object", object); verifyParameterPresence("objectType", objectType); return jsonMapper.toJavaObject(makeRequest(object, parameters), objectType); }
/** * @see com.restfb.FacebookClient#fetchConnection(String, * Class, com.restfb.Parameter[]) */ @Override public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) { verifyParameterPresence("connection", connection); verifyParameterPresence("connectionType", connectionType); return mapToConnection(makeRequest(connection, parameters), connectionType); }
/** * @see com.restfb.FacebookClient#executeQuery(String, * Class, com.restfb.Parameter[]) */ @Override public <T> List<T> executeQuery(String query, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("query", query); verifyParameterPresence("objectType", objectType); for (Parameter parameter : parameters) if (QUERY_PARAM_NAME.equals(parameter.name)) throw new IllegalArgumentException("You cannot specify the '" + QUERY_PARAM_NAME + "' URL parameter yourself - " + "RestFB will populate this for you with " + "the query you passed to this method."); return jsonMapper.toJavaList( makeRequest("fql.query", true, false, false, null, parametersWithAdditionalParameter(Parameter.with(QUERY_PARAM_NAME, query), parameters)), objectType); }
@Override public DeviceCode fetchDeviceCode(ScopeBuilder scope) { verifyParameterPresence("scope", scope); if (accessToken == null) { throw new IllegalStateException("access token is required to fetch a device access token"); } String response = makeRequest("device/login", true, false, null, Parameter.with("type", "device_code"), Parameter.with("scope", scope.toString())); return jsonMapper.toJavaObject(response, DeviceCode.class); }
@Override public AccessToken obtainDeviceAccessToken(String code) throws FacebookDeviceTokenCodeExpiredException, FacebookDeviceTokenPendingException, FacebookDeviceTokenDeclinedException, FacebookDeviceTokenSlowdownException { verifyParameterPresence("code", code); if (accessToken == null) { throw new IllegalStateException("access token is required to fetch a device access token"); } try { String response = makeRequest("device/login_status", true, false, null, Parameter.with("type", "device_token"), Parameter.with("code", code)); return getAccessTokenFromResponse(response); } catch (FacebookOAuthException foae) { DeviceTokenExceptionFactory.createFrom(foae); return null; } }
/** * @see com.restfb.FacebookClient#convertSessionKeysToAccessTokens(java.lang.String, java.lang.String, * java.lang.String[]) */ @Override public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) { verifyParameterPresence("appId", appId); verifyParameterPresence("secretKey", secretKey); if (sessionKeys == null || sessionKeys.length == 0) { return emptyList(); } String json = makeRequest("/oauth/exchange_sessions", true, false, null, Parameter.with("client_id", appId), Parameter.with("client_secret", secretKey), Parameter.with("sessions", join(sessionKeys))); return jsonMapper.toJavaList(json, AccessToken.class); }
/** * @see com.restfb.FacebookClient#convertSessionKeysToAccessTokens(String, * String, String[]) */ @Override public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) { verifyParameterPresence("appId", appId); verifyParameterPresence("secretKey", secretKey); if (sessionKeys == null || sessionKeys.length == 0) return emptyList(); String json = makeRequest("/oauth/exchange_sessions", false, true, false, null, Parameter.with("client_id", appId), Parameter.with("client_secret", secretKey), Parameter.with("sessions", join(sessionKeys))); return jsonMapper.toJavaList(json, AccessToken.class); }
/** * @see com.restfb.FacebookClient#obtainAppAccessToken(java.lang.String, java.lang.String) */ @Override public AccessToken obtainAppAccessToken(String appId, String appSecret) { verifyParameterPresence("appId", appId); verifyParameterPresence("appSecret", appSecret); String response = makeRequest("oauth/access_token", Parameter.with("grant_type", "client_credentials"), Parameter.with("client_id", appId), Parameter.with("client_secret", appSecret)); try { return getAccessTokenFromResponse(response); } catch (Exception t) { throw new FacebookResponseContentException("Unable to extract access token from response.", t); } }
/** * @see com.restfb.FacebookClient#obtainUserAccessToken(java.lang.String, java.lang.String, java.lang.String, * java.lang.String) */ @Override public AccessToken obtainUserAccessToken(String appId, String appSecret, String redirectUri, String verificationCode) { verifyParameterPresence("appId", appId); verifyParameterPresence("appSecret", appSecret); verifyParameterPresence("verificationCode", verificationCode); String response = makeRequest("oauth/access_token", Parameter.with("client_id", appId), Parameter.with("client_secret", appSecret), Parameter.with("code", verificationCode), Parameter.with("redirect_uri", redirectUri)); try { return getAccessTokenFromResponse(response); } catch (Exception t) { throw new FacebookResponseContentException("Unable to extract access token from response.", t); } }
/** * @see com.restfb.FacebookClient#obtainExtendedAccessToken(java.lang.String, java.lang.String, java.lang.String) */ @Override public AccessToken obtainExtendedAccessToken(String appId, String appSecret, String accessToken) { verifyParameterPresence("appId", appId); verifyParameterPresence("appSecret", appSecret); verifyParameterPresence("accessToken", accessToken); String response = makeRequest("/oauth/access_token", false, false, null, Parameter.with("client_id", appId), Parameter.with("client_secret", appSecret), Parameter.with("grant_type", "fb_exchange_token"), Parameter.with("fb_exchange_token", accessToken)); try { return getAccessTokenFromResponse(response); } catch (Exception t) { throw new FacebookResponseContentException("Unable to extract access token from response.", t); } }
@Override public boolean deleteObject(String object, Parameter... parameters) { verifyParameterPresence("object", object); String responseString = makeRequest(object, true, true, null, parameters); try { JsonValue jObj = Json.parse(responseString); boolean success = false; if (jObj.isObject()) { if (jObj.asObject().get("success") != null) { success = jObj.asObject().get("success").asBoolean(); } if (jObj.asObject().get("result") != null) { success = jObj.asObject().get("result").asString().contains("Successfully deleted"); } } else { success = jObj.asBoolean(); } return success; } catch (ParseException jex) { CLIENT_LOGGER.trace("no valid JSON returned while deleting a object, using returned String instead", jex); return "true".equals(responseString); } }
/** * @see com.restfb.FacebookClient#debugToken(java.lang.String) */ @Override public DebugTokenInfo debugToken(String inputToken) { verifyParameterPresence("inputToken", inputToken); String response = makeRequest("/debug_token", Parameter.with("input_token", inputToken)); try { JsonObject json = Json.parse(response).asObject(); JsonObject data = json.get("data").asObject(); return getJsonMapper().toJavaObject(data.toString(), DebugTokenInfo.class); } catch (Exception t) { throw new FacebookResponseContentException("Unable to parse JSON from response.", t); } }
/** * @see com.restfb.FacebookClient#fetchConnection(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) { verifyParameterPresence("connection", connection); verifyParameterPresence("connectionType", connectionType); Connection<T> conn = new Connection<>(this, makeRequest(connection, parameters), connectionType); if (conn.getNextPageUrl() == null && conn.getAfterCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "after", conn.getAfterCursor()); conn.setNextPageUrl(fullUrl); } if (conn.getPreviousPageUrl() == null && conn.getBeforeCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "before", conn.getBeforeCursor()); conn.setPreviousPageUrl(fullUrl); } return conn; }