Search in sources :

Example 1 with GraphUser

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

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

the class UserSettingsFragment method fetchUserInfo.

private void fetchUserInfo() {
    final Session currentSession = getSession();
    if (currentSession != null && currentSession.isOpened()) {
        if (currentSession != userInfoSession) {
            Request request = Request.newMeRequest(currentSession, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser me, Response response) {
                    if (currentSession == getSession()) {
                        user = me;
                        updateUI();
                    }
                    if (response.getError() != null) {
                        loginButton.handleError(response.getError().getException());
                    }
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString(FIELDS, REQUEST_FIELDS);
            request.setParameters(parameters);
            Request.executeBatchAsync(request);
            userInfoSession = currentSession;
        }
    } else {
        user = null;
    }
}
Also used : ImageResponse(com.facebook.internal.ImageResponse) GraphUser(com.facebook.model.GraphUser) Bundle(android.os.Bundle) ImageRequest(com.facebook.internal.ImageRequest)

Example 3 with GraphUser

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

the class FriendPickerFragment method setSelection.

/**
 * Sets the list of friends for pre selection. These friends will be selected by default.
 * @param graphUsers list of friends as GraphUsers
 */
public void setSelection(List<GraphUser> graphUsers) {
    List<String> userIds = new ArrayList<String>();
    for (GraphUser graphUser : graphUsers) {
        userIds.add(graphUser.getId());
    }
    setSelectionByIds(userIds);
}
Also used : GraphUser(com.facebook.model.GraphUser)

Example 4 with GraphUser

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

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> grantedPermissions = new ArrayList<String>();
    final ArrayList<String> declinedPermissions = 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 {
                Session.PermissionsPair permissionsPair = Session.handlePermissionResponse(response);
                if (permissionsPair != null) {
                    grantedPermissions.addAll(permissionsPair.getGrantedPermissions());
                    declinedPermissions.addAll(permissionsPair.getDeclinedPermissions());
                }
            } 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, grantedPermissions, declinedPermissions);
                    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 : GraphUser(com.facebook.model.GraphUser) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 5 with GraphUser

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

the class TestSession method populateTestAccounts.

private static synchronized void populateTestAccounts(Collection<TestAccount> testAccounts, GraphObject userAccountsMap) {
    for (TestAccount testAccount : testAccounts) {
        GraphUser testUser = userAccountsMap.getPropertyAs(testAccount.getId(), GraphUser.class);
        testAccount.setName(testUser.getName());
        storeTestAccount(testAccount);
    }
}
Also used : GraphUser(com.facebook.model.GraphUser)

Aggregations

GraphUser (com.facebook.model.GraphUser)23 Bundle (android.os.Bundle)6 Response (com.facebook.Response)6 Session (com.facebook.Session)6 Request (com.facebook.Request)5 JSONException (org.json.JSONException)5 ActivityNotFoundException (android.content.ActivityNotFoundException)4 ImageRequest (com.facebook.internal.ImageRequest)4 ImageResponse (com.facebook.internal.ImageResponse)4 ArrayList (java.util.ArrayList)4 GraphUserCallback (com.facebook.Request.GraphUserCallback)3 GraphUserListCallback (com.facebook.Request.GraphUserListCallback)3 GraphObject (com.facebook.model.GraphObject)3 FacebookAuthorizationException (com.facebook.FacebookAuthorizationException)2 FacebookOperationCanceledException (com.facebook.FacebookOperationCanceledException)2 FacebookRequestError (com.facebook.FacebookRequestError)2 GraphMultiResult (com.facebook.model.GraphMultiResult)2 GraphObjectList (com.facebook.model.GraphObjectList)2 Random (java.util.Random)2 View (android.view.View)1