Search in sources :

Example 1 with GraphObjectList

use of com.facebook.model.GraphObjectList in project openkit-android by OpenKit.

the class AuthorizationClient method createReauthValidationBatch.

RequestBatch createReauthValidationBatch(final Result pendingResult) {
    // We need to ensure that the token we got represents the same fbid as the old one. We issue
    // a "me" request using the current token, a "me" request using the new token, and a "me/permissions"
    // request using the current token to get the permissions of the user.
    final ArrayList<String> fbids = new ArrayList<String>();
    final ArrayList<String> tokenPermissions = new ArrayList<String>();
    final String newToken = pendingResult.token.getToken();
    Request.Callback meCallback = new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphUser user = response.getGraphObjectAs(GraphUser.class);
                if (user != null) {
                    fbids.add(user.getId());
                }
            } catch (Exception ex) {
            }
        }
    };
    String validateSameFbidAsToken = pendingRequest.getPreviousAccessToken();
    Request requestCurrentTokenMe = createGetProfileIdRequest(validateSameFbidAsToken);
    requestCurrentTokenMe.setCallback(meCallback);
    Request requestNewTokenMe = createGetProfileIdRequest(newToken);
    requestNewTokenMe.setCallback(meCallback);
    Request requestCurrentTokenPermissions = createGetPermissionsRequest(validateSameFbidAsToken);
    requestCurrentTokenPermissions.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
                if (result != null) {
                    GraphObjectList<GraphObject> data = result.getData();
                    if (data != null && data.size() == 1) {
                        GraphObject permissions = data.get(0);
                        // The keys are the permission names.
                        tokenPermissions.addAll(permissions.asMap().keySet());
                    }
                }
            } catch (Exception ex) {
            }
        }
    });
    RequestBatch batch = new RequestBatch(requestCurrentTokenMe, requestNewTokenMe, requestCurrentTokenPermissions);
    batch.setBatchApplicationId(pendingRequest.getApplicationId());
    batch.addCallback(new RequestBatch.Callback() {

        @Override
        public void onBatchCompleted(RequestBatch batch) {
            try {
                Result result = null;
                if (fbids.size() == 2 && fbids.get(0) != null && fbids.get(1) != null && fbids.get(0).equals(fbids.get(1))) {
                    // Modify the token to have the right permission set.
                    AccessToken tokenWithPermissions = AccessToken.createFromTokenWithRefreshedPermissions(pendingResult.token, tokenPermissions);
                    result = Result.createTokenResult(pendingRequest, tokenWithPermissions);
                } else {
                    result = Result.createErrorResult(pendingRequest, "User logged in as different Facebook user.", null);
                }
                complete(result);
            } catch (Exception ex) {
                complete(Result.createErrorResult(pendingRequest, "Caught exception", ex.getMessage()));
            } finally {
                notifyBackgroundProcessingStop();
            }
        }
    });
    return batch;
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) GraphUser(com.facebook.model.GraphUser) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) ActivityNotFoundException(android.content.ActivityNotFoundException) GraphMultiResult(com.facebook.model.GraphMultiResult) GraphMultiResult(com.facebook.model.GraphMultiResult)

Example 2 with GraphObjectList

use of com.facebook.model.GraphObjectList in project facebook-api-android-maven by avianey.

the class Response method createResponseFromObject.

private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object, boolean isFromCache, Object originalResult) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;
        FacebookRequestError error = FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
        if (error != null) {
            if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
                Session session = request.getSession();
                if (session != null) {
                    session.closeAndClearTokenInformation();
                }
            }
            return new Response(request, connection, error);
        }
        Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);
        if (body instanceof JSONObject) {
            GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
            return new Response(request, connection, body.toString(), graphObject, isFromCache);
        } else if (body instanceof JSONArray) {
            GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList((JSONArray) body, GraphObject.class);
            return new Response(request, connection, body.toString(), graphObjectList, isFromCache);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }
    if (object == JSONObject.NULL) {
        return new Response(request, connection, object.toString(), (GraphObject) null, isFromCache);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: " + object.getClass().getSimpleName());
    }
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) GraphObject(com.facebook.model.GraphObject)

Example 3 with GraphObjectList

use of com.facebook.model.GraphObjectList in project phonegap-facebook-plugin by Wizcorp.

the class Response method createResponseFromObject.

private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object, boolean isFromCache, Object originalResult) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;
        FacebookRequestError error = FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
        if (error != null) {
            if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
                Session session = request.getSession();
                if (session != null) {
                    session.closeAndClearTokenInformation();
                }
            }
            return new Response(request, connection, error);
        }
        Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);
        if (body instanceof JSONObject) {
            GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
            return new Response(request, connection, body.toString(), graphObject, isFromCache);
        } else if (body instanceof JSONArray) {
            GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList((JSONArray) body, GraphObject.class);
            return new Response(request, connection, body.toString(), graphObjectList, isFromCache);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }
    if (object == JSONObject.NULL) {
        return new Response(request, connection, object.toString(), (GraphObject) null, isFromCache);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: " + object.getClass().getSimpleName());
    }
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) GraphObject(com.facebook.model.GraphObject)

Example 4 with GraphObjectList

use of com.facebook.model.GraphObjectList in project openkit-android by OpenKit.

the class Response method createResponseFromObject.

private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object, boolean isFromCache, Object originalResult) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;
        FacebookRequestError error = FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
        if (error != null) {
            if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
                Session session = request.getSession();
                if (session != null) {
                    session.closeAndClearTokenInformation();
                }
            }
            return new Response(request, connection, error);
        }
        Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);
        if (body instanceof JSONObject) {
            GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
            return new Response(request, connection, graphObject, isFromCache);
        } else if (body instanceof JSONArray) {
            GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList((JSONArray) body, GraphObject.class);
            return new Response(request, connection, graphObjectList, isFromCache);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }
    if (object == JSONObject.NULL) {
        return new Response(request, connection, (GraphObject) null, isFromCache);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: " + object.getClass().getSimpleName());
    }
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) GraphObject(com.facebook.model.GraphObject)

Example 5 with GraphObjectList

use of com.facebook.model.GraphObjectList in project Klyph by jonathangerbaud.

the class AuthorizationClient method createReauthValidationBatch.

RequestBatch createReauthValidationBatch(final Result pendingResult) {
    // We need to ensure that the token we got represents the same fbid as the old one. We issue
    // a "me" request using the current token, a "me" request using the new token, and a "me/permissions"
    // request using the current token to get the permissions of the user.
    final ArrayList<String> fbids = new ArrayList<String>();
    final ArrayList<String> tokenPermissions = new ArrayList<String>();
    final String newToken = pendingResult.token.getToken();
    Request.Callback meCallback = new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphUser user = response.getGraphObjectAs(GraphUser.class);
                if (user != null) {
                    fbids.add(user.getId());
                }
            } catch (Exception ex) {
            }
        }
    };
    String validateSameFbidAsToken = pendingRequest.getPreviousAccessToken();
    Request requestCurrentTokenMe = createGetProfileIdRequest(validateSameFbidAsToken);
    requestCurrentTokenMe.setCallback(meCallback);
    Request requestNewTokenMe = createGetProfileIdRequest(newToken);
    requestNewTokenMe.setCallback(meCallback);
    Request requestCurrentTokenPermissions = createGetPermissionsRequest(validateSameFbidAsToken);
    requestCurrentTokenPermissions.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            try {
                GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
                if (result != null) {
                    GraphObjectList<GraphObject> data = result.getData();
                    if (data != null && data.size() == 1) {
                        GraphObject permissions = data.get(0);
                        // The keys are the permission names.
                        tokenPermissions.addAll(permissions.asMap().keySet());
                    }
                }
            } catch (Exception ex) {
            }
        }
    });
    RequestBatch batch = new RequestBatch(requestCurrentTokenMe, requestNewTokenMe, requestCurrentTokenPermissions);
    batch.setBatchApplicationId(pendingRequest.getApplicationId());
    batch.addCallback(new RequestBatch.Callback() {

        @Override
        public void onBatchCompleted(RequestBatch batch) {
            try {
                Result result = null;
                if (fbids.size() == 2 && fbids.get(0) != null && fbids.get(1) != null && fbids.get(0).equals(fbids.get(1))) {
                    // Modify the token to have the right permission set.
                    AccessToken tokenWithPermissions = AccessToken.createFromTokenWithRefreshedPermissions(pendingResult.token, tokenPermissions);
                    result = Result.createTokenResult(pendingRequest, tokenWithPermissions);
                } else {
                    result = Result.createErrorResult(pendingRequest, "User logged in as different Facebook user.", null);
                }
                complete(result);
            } catch (Exception ex) {
                complete(Result.createErrorResult(pendingRequest, "Caught exception", ex.getMessage()));
            } finally {
                notifyBackgroundProcessingStop();
            }
        }
    });
    return batch;
}
Also used : GraphObjectList(com.facebook.model.GraphObjectList) GraphUser(com.facebook.model.GraphUser) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) ActivityNotFoundException(android.content.ActivityNotFoundException) GraphMultiResult(com.facebook.model.GraphMultiResult) GraphMultiResult(com.facebook.model.GraphMultiResult)

Aggregations

GraphObject (com.facebook.model.GraphObject)6 GraphObjectList (com.facebook.model.GraphObjectList)6 JSONArray (org.json.JSONArray)4 JSONObject (org.json.JSONObject)4 ActivityNotFoundException (android.content.ActivityNotFoundException)2 GraphMultiResult (com.facebook.model.GraphMultiResult)2 GraphUser (com.facebook.model.GraphUser)2 JSONException (org.json.JSONException)2