RestFbFacebookPhotos(TokensAndUrlAuthData authData, AppCredentials appCredentials) { client = new DefaultFacebookClient( authData.getAccessToken(), appCredentials.getSecret(), Version.VERSION_3_0); }
/** * Get user name from Facebook, but the code provided. * @param token Facebook access token * @return The user found in FB */ private User fetch(final String token) { try { return new DefaultFacebookClient(token) .fetchObject("me", User.class); } catch (final FacebookException ex) { throw new IllegalArgumentException(ex); } }
verifyParameterLegality(parameters); parameters = parametersWithAdditionalParameter(Parameter.with(METHOD_PARAM_NAME, "delete"), parameters); (useLegacyEndpoint ? getFacebookLegacyEndpointUrl() : getFacebookGraphEndpointUrl()) + endpoint; final String parameterString = toParameterString(parameters); return makeRequestAndProcessResponse(new Requestor() {
String appSecret = env.get("facebook.appSecret"); try { FacebookClient facebookClient = new DefaultFacebookClient(access_token); User user = facebookClient.fetchObject("me", User.class); if (guest==null) { FacebookClient.AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken(appId, appSecret, access_token); String firstname = user.getFirstName(); String lastname = user.getLastName();
verifyParameterLegality(parameters); if (executeAsDelete && isHttpDeleteFallback()) { parameters = parametersWithAdditionalParameter(Parameter.with(METHOD_PARAM_NAME, "delete"), parameters); createEndpointForApiCall(endpoint, binaryAttachments != null && !binaryAttachments.isEmpty()); final String parameterString = toParameterString(parameters); return makeRequestAndProcessResponse(new Requestor() {
/** * @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> T publish(String connection, Class<T> objectType, Parameter... parameters) { return publish(connection, objectType, (List<BinaryAttachment>) null, parameters); }
/** * @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); }
@SuppressWarnings("unchecked") public <T> T executeMultiquery(Map<String, String> queries, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("objectType", objectType); new JsonArray(makeRequest("fql.multiquery", true, false, false, null, parametersWithAdditionalParameter(Parameter.with(QUERIES_PARAM_NAME, queriesToJson(queries)), 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)); }
@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; } }
/** * 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); }
/** * @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#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; }
/** * @see com.restfb.BaseFacebookClient#createEndpointForApiCall(java.lang.String,boolean) */ @Override protected String createEndpointForApiCall(String apiCall, boolean hasAttachment) { while (apiCall.startsWith("/")) apiCall = apiCall.substring(1); String baseUrl = getFacebookGraphEndpointUrl(); if (hasAttachment && (apiCall.endsWith("/videos") || apiCall.endsWith("/advideos"))) { baseUrl = getFacebookGraphVideoEndpointUrl(); } else if (apiCall.endsWith("logout.php")) { baseUrl = getFacebookEndpointUrls().getFacebookEndpoint(); } return format("%s/%s", baseUrl, apiCall); }
String jsonString = makeRequest("", parametersWithAdditionalParameter(Parameter.with(IDS_PARAM_NAME, idArray.toString()), parameters));
/** * @see com.restfb.FacebookClient#fetchConnectionPage(String, * Class) */ @Override public <T> Connection<T> fetchConnectionPage(final String connectionPageUrl, Class<T> connectionType) { String connectionJson = makeRequestAndProcessResponse(new Requestor() { /** * @see com.restfb.DefaultFacebookClient.Requestor#makeRequest() */ @Override public Response makeRequest() throws IOException { return webRequestor.executeGet(connectionPageUrl); } }); return mapToConnection(connectionJson, connectionType); }
/** * @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); } }
String connectionJson; if (!isBlank(accessToken) && !isBlank(appSecret)) { connectionJson = makeRequestAndProcessResponse(new Requestor() { @Override public Response makeRequest() throws IOException { connectionJson = makeRequestAndProcessResponse(new Requestor() { @Override public Response makeRequest() throws IOException {
@Override protected Try<User> run() throws Exception { DefaultFacebookClient fb = new DefaultFacebookClient(accessToken, Version.VERSION_2_4); return Try.of(() -> fb.fetchObject("me", JsonObject.class)) .map(this::createUser); }
String uid = "..."; DefaultFacebookClient client = ...; FacebookType postId = client.publish( uid + "/feed", FacebookType.class, Parameter.with("message", "Hello, world"), Parameter.with("link", "http://www.google.com"));