Java Code Examples for com.facebook.GraphResponse#getError()

The following examples show how to use com.facebook.GraphResponse#getError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utility.java    From kognitivo with Apache License 2.0 6 votes vote down vote up
public static void getGraphMeRequestWithCacheAsync(
        final String accessToken,
        final GraphMeRequestWithCacheCallback callback) {
    JSONObject cachedValue = ProfileInformationCache.getProfileInformation(accessToken);
    if (cachedValue != null) {
        callback.onSuccess(cachedValue);
        return;
    }

    GraphRequest.Callback graphCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            if (response.getError() != null) {
                callback.onFailure(response.getError().getException());
            } else {
                ProfileInformationCache.putProfileInformation(
                        accessToken,
                        response.getJSONObject());
                callback.onSuccess(response.getJSONObject());
            }
        }
    };
    GraphRequest graphRequest = getGraphMeRequestWithCache(accessToken);
    graphRequest.setCallback(graphCallback);
    graphRequest.executeAsync();
}
 
Example 2
Source File: ShareInternalUtility.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
public static void invokeCallbackWithResults(
        FacebookCallback<Sharer.Result> callback,
        final String postId,
        final GraphResponse graphResponse) {
    FacebookRequestError requestError = graphResponse.getError();
    if (requestError != null) {
        String errorMessage = requestError.getErrorMessage();
        if (Utility.isNullOrEmpty(errorMessage)) {
            errorMessage = "Unexpected error sharing.";
        }
        invokeOnErrorCallback(callback, graphResponse, errorMessage);
    } else {
        invokeOnSuccessCallback(callback, postId);
    }
}
 
Example 3
Source File: VideoUploader.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
protected void executeGraphRequestSynchronously(Bundle parameters) {
    GraphRequest request = new GraphRequest(
            uploadContext.accessToken,
            String.format(Locale.ROOT, "%s/videos", uploadContext.graphNode),
            parameters,
            HttpMethod.POST,
            null);
    GraphResponse response = request.executeAndWait();

    if (response != null) {
        FacebookRequestError error = response.getError();
        JSONObject responseJSON = response.getJSONObject();
        if (error != null) {
            if (!attemptRetry(error.getSubErrorCode())) {
                handleError(new FacebookGraphResponseException(response, ERROR_UPLOAD));
            }
        } else if (responseJSON != null) {
            try {
                handleSuccess(responseJSON);
            } catch (JSONException e) {
                endUploadWithFailure(new FacebookException(ERROR_BAD_SERVER_RESPONSE, e));
            }
        } else {
            handleError(new FacebookException(ERROR_BAD_SERVER_RESPONSE));
        }
    } else {
        handleError(new FacebookException(ERROR_BAD_SERVER_RESPONSE));
    }
}
 
Example 4
Source File: LoginClient.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
/**
 * This parses a server response to a call to me/permissions.  It will return the list of
 * granted permissions. It will optionally update an access token with the requested permissions.
 *
 * @param response The server response
 * @return A list of granted permissions or null if an error
 */
private static PermissionsPair handlePermissionResponse(GraphResponse response) {
    if (response.getError() != null) {
        return null;
    }

    JSONObject result = response.getJSONObject();
    if (result == null) {
        return null;
    }

    JSONArray data = result.optJSONArray("data");
    if (data == null || data.length() == 0) {
        return null;
    }
    List<String> grantedPermissions = new ArrayList<String>(data.length());
    List<String> declinedPermissions = new ArrayList<String>(data.length());

    for (int i = 0; i < data.length(); ++i) {
        JSONObject object = data.optJSONObject(i);
        String permission = object.optString("permission");
        if (permission == null || permission.equals("installed")) {
            continue;
        }
        String status = object.optString("status");
        if (status == null) {
            continue;
        }
        if(status.equals("granted")) {
            grantedPermissions.add(permission);
        } else if (status.equals("declined")) {
            declinedPermissions.add(permission);
        }
    }

    return new PermissionsPair(grantedPermissions, declinedPermissions);
}
 
Example 5
Source File: Utility.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
public static JSONObject awaitGetGraphMeRequestWithCache(
        final String accessToken) {
    JSONObject cachedValue = ProfileInformationCache.getProfileInformation(accessToken);
    if (cachedValue != null) {
        return cachedValue;
    }

    GraphRequest graphRequest = getGraphMeRequestWithCache(accessToken);
    GraphResponse response = graphRequest.executeAndWait();
    if (response.getError() != null) {
        return null;
    }

    return response.getJSONObject();
}
 
Example 6
Source File: ShareApi.java    From kognitivo with Apache License 2.0 4 votes vote down vote up
private void sharePhotoContent(final SharePhotoContent photoContent,
                               final FacebookCallback<Sharer.Result> callback) {
    final Mutable<Integer> requestCount = new Mutable<Integer>(0);
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    final ArrayList<GraphRequest> requests = new ArrayList<GraphRequest>();
    final ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    final ArrayList<GraphResponse> errorResponses = new ArrayList<GraphResponse>();
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject result = response.getJSONObject();
            if (result != null) {
                results.add(result);
            }
            if (response.getError() != null) {
                errorResponses.add(response);
            }
            requestCount.value -= 1;
            if (requestCount.value == 0) {
                if (!errorResponses.isEmpty()) {
                    ShareInternalUtility.invokeCallbackWithResults(
                            callback,
                            null,
                            errorResponses.get(0));
                } else if (!results.isEmpty()) {
                    final String postId = results.get(0).optString("id");
                    ShareInternalUtility.invokeCallbackWithResults(
                            callback,
                            postId,
                            response);
                }
            }
        }
    };
    try {
        for (SharePhoto photo : photoContent.getPhotos()) {
            final Bitmap bitmap = photo.getBitmap();
            final Uri photoUri = photo.getImageUrl();
            String caption = photo.getCaption();
            if (caption == null) {
                caption = this.getMessage();
            }
            if (bitmap != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(
                        accessToken,
                        getGraphPath(PHOTOS_EDGE),
                        bitmap,
                        caption,
                        photo.getParameters(),
                        requestCallback));
            } else if (photoUri != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(
                        accessToken,
                        getGraphPath(PHOTOS_EDGE),
                        photoUri,
                        caption,
                        photo.getParameters(),
                        requestCallback));
            }
        }
        requestCount.value += requests.size();
        for (GraphRequest request : requests) {
            request.executeAsync();
        }
    } catch (final FileNotFoundException ex) {
        ShareInternalUtility.invokeCallbackWithException(callback, ex);
    }
}
 
Example 7
Source File: AppEventsLogger.java    From kognitivo with Apache License 2.0 4 votes vote down vote up
private static void handleResponse(
        AccessTokenAppIdPair accessTokenAppId,
        GraphRequest request,
        GraphResponse response,
        SessionEventsState sessionEventsState,
        FlushStatistics flushState) {
    FacebookRequestError error = response.getError();
    String resultDescription = "Success";

    FlushResult flushResult = FlushResult.SUCCESS;

    if (error != null) {
        final int NO_CONNECTIVITY_ERROR_CODE = -1;
        if (error.getErrorCode() == NO_CONNECTIVITY_ERROR_CODE) {
            resultDescription = "Failed: No Connectivity";
            flushResult = FlushResult.NO_CONNECTIVITY;
        } else {
            resultDescription = String.format("Failed:\n  Response: %s\n  Error %s",
                    response.toString(),
                    error.toString());
            flushResult = FlushResult.SERVER_ERROR;
        }
    }

    if (FacebookSdk.isLoggingBehaviorEnabled(LoggingBehavior.APP_EVENTS)) {
        String eventsJsonString = (String) request.getTag();
        String prettyPrintedEvents;

        try {
            JSONArray jsonArray = new JSONArray(eventsJsonString);
            prettyPrintedEvents = jsonArray.toString(2);
        } catch (JSONException exc) {
            prettyPrintedEvents = "<Can't encode events for debug logging>";
        }

        Logger.log(LoggingBehavior.APP_EVENTS, TAG,
                "Flush completed\nParams: %s\n  Result: %s\n  Events JSON: %s",
                request.getGraphObject().toString(),
                resultDescription,
                prettyPrintedEvents);
    }

    sessionEventsState.clearInFlightAndStats(error != null);

    if (flushResult == FlushResult.NO_CONNECTIVITY) {
        // We may call this for multiple requests in a batch, which is slightly inefficient
        // since in principle we could call it once for all failed requests, but the impact is
        // likely to be minimal. We don't call this for other server errors, because if an event
        // failed because it was malformed, etc., continually retrying it will cause subsequent
        // events to not be logged either.
        PersistedEvents.persistEvents(applicationContext, accessTokenAppId, sessionEventsState);
    }

    if (flushResult != FlushResult.SUCCESS) {
        // We assume that connectivity issues are more significant to report than server issues.
        if (flushState.result != FlushResult.NO_CONNECTIVITY) {
            flushState.result = flushResult;
        }
    }
}