Search in sources :

Example 1 with GraphMultiResult

use of com.facebook.model.GraphMultiResult 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 GraphMultiResult

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

the class Session method handlePermissionResponse.

/**
     * This parses a server response to a call to me/permissions.  It will return the list of granted permissions.
     * It will optionally update a session with the requested permissions.  It also handles the distinction between
     * 1.0 and 2.0 calls to the endpoint.
     *
     * @param response The server response
     * @return A list of granted permissions or null if an error
     */
static PermissionsPair handlePermissionResponse(Response response) {
    if (response.getError() != null) {
        return null;
    }
    GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
    if (result == null) {
        return null;
    }
    GraphObjectList<GraphObject> data = result.getData();
    if (data == null || data.size() == 0) {
        return null;
    }
    List<String> grantedPermissions = new ArrayList<String>(data.size());
    List<String> declinedPermissions = new ArrayList<String>(data.size());
    // Check if we are dealing with v2.0 or v1.0 behavior until the server is updated
    GraphObject firstObject = data.get(0);
    if (firstObject.getProperty("permission") != null) {
        // v2.0
        for (GraphObject graphObject : data) {
            String permission = (String) graphObject.getProperty("permission");
            if (permission.equals("installed")) {
                continue;
            }
            String status = (String) graphObject.getProperty("status");
            if (status.equals("granted")) {
                grantedPermissions.add(permission);
            } else if (status.equals("declined")) {
                declinedPermissions.add(permission);
            }
        }
    } else {
        // v1.0
        Map<String, Object> permissionsMap = firstObject.asMap();
        for (Map.Entry<String, Object> entry : permissionsMap.entrySet()) {
            if (entry.getKey().equals("installed")) {
                continue;
            }
            if ((Integer) entry.getValue() == 1) {
                grantedPermissions.add(entry.getKey());
            }
        }
    }
    return new PermissionsPair(grantedPermissions, declinedPermissions);
}
Also used : GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) GraphMultiResult(com.facebook.model.GraphMultiResult) GraphObject(com.facebook.model.GraphObject)

Example 3 with GraphMultiResult

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

the class Session method handlePermissionResponse.

/**
     * This parses a server response to a call to me/permissions.  It will return the list of granted permissions.
     * It will optionally update a session with the requested permissions.  It also handles the distinction between
     * 1.0 and 2.0 calls to the endpoint.
     *
     * @param response The server response
     * @return A list of granted permissions or null if an error
     */
static PermissionsPair handlePermissionResponse(Response response) {
    if (response.getError() != null) {
        return null;
    }
    GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
    if (result == null) {
        return null;
    }
    GraphObjectList<GraphObject> data = result.getData();
    if (data == null || data.size() == 0) {
        return null;
    }
    List<String> grantedPermissions = new ArrayList<String>(data.size());
    List<String> declinedPermissions = new ArrayList<String>(data.size());
    // Check if we are dealing with v2.0 or v1.0 behavior until the server is updated
    GraphObject firstObject = data.get(0);
    if (firstObject.getProperty("permission") != null) {
        // v2.0
        for (GraphObject graphObject : data) {
            String permission = (String) graphObject.getProperty("permission");
            if (permission.equals("installed")) {
                continue;
            }
            String status = (String) graphObject.getProperty("status");
            if (status.equals("granted")) {
                grantedPermissions.add(permission);
            } else if (status.equals("declined")) {
                declinedPermissions.add(permission);
            }
        }
    } else {
        // v1.0
        Map<String, Object> permissionsMap = firstObject.asMap();
        for (Map.Entry<String, Object> entry : permissionsMap.entrySet()) {
            if (entry.getKey().equals("installed")) {
                continue;
            }
            if ((Integer) entry.getValue() == 1) {
                grantedPermissions.add(entry.getKey());
            }
        }
    }
    return new PermissionsPair(grantedPermissions, declinedPermissions);
}
Also used : GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject) GraphMultiResult(com.facebook.model.GraphMultiResult) GraphObject(com.facebook.model.GraphObject)

Example 4 with GraphMultiResult

use of com.facebook.model.GraphMultiResult 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

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