Search in sources :

Example 26 with AccessToken

use of com.facebook.AccessToken in project facebook-android-sdk by facebook.

the class WebLoginMethodHandler method onComplete.

protected void onComplete(LoginClient.Request request, Bundle values, FacebookException error) {
    LoginClient.Result outcome;
    e2e = null;
    if (values != null) {
        // Actual e2e we got from the dialog should be used for logging.
        if (values.containsKey(ServerProtocol.DIALOG_PARAM_E2E)) {
            e2e = values.getString(ServerProtocol.DIALOG_PARAM_E2E);
        }
        try {
            AccessToken token = createAccessTokenFromWebBundle(request.getPermissions(), values, getTokenSource(), request.getApplicationId());
            outcome = LoginClient.Result.createTokenResult(loginClient.getPendingRequest(), token);
            // Ensure any cookies set by the dialog are saved
            // This is to work around a bug where CookieManager may fail to instantiate if
            // CookieSyncManager has never been created.
            CookieSyncManager syncManager = CookieSyncManager.createInstance(loginClient.getActivity());
            syncManager.sync();
            saveCookieToken(token.getToken());
        } catch (FacebookException ex) {
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null, ex.getMessage());
        }
    } else {
        if (error instanceof FacebookOperationCanceledException) {
            outcome = LoginClient.Result.createCancelResult(loginClient.getPendingRequest(), "User canceled log in.");
        } else {
            // Something went wrong, don't log a completion event since it will skew timing
            // results.
            e2e = null;
            String errorCode = null;
            String errorMessage = error.getMessage();
            if (error instanceof FacebookServiceException) {
                FacebookRequestError requestError = ((FacebookServiceException) error).getRequestError();
                errorCode = String.format(Locale.ROOT, "%d", requestError.getErrorCode());
                errorMessage = requestError.toString();
            }
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null, errorMessage, errorCode);
        }
    }
    if (!Utility.isNullOrEmpty(e2e)) {
        logWebLoginCompleted(e2e);
    }
    loginClient.completeAndValidate(outcome);
}
Also used : FacebookOperationCanceledException(com.facebook.FacebookOperationCanceledException) AccessToken(com.facebook.AccessToken) CookieSyncManager(android.webkit.CookieSyncManager) FacebookException(com.facebook.FacebookException) FacebookServiceException(com.facebook.FacebookServiceException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 27 with AccessToken

use of com.facebook.AccessToken in project facebook-android-sdk by facebook.

the class WebLoginMethodHandler method getParameters.

protected Bundle getParameters(final LoginClient.Request request) {
    Bundle parameters = new Bundle();
    if (!Utility.isNullOrEmpty(request.getPermissions())) {
        String scope = TextUtils.join(",", request.getPermissions());
        parameters.putString(ServerProtocol.DIALOG_PARAM_SCOPE, scope);
        addLoggingExtra(ServerProtocol.DIALOG_PARAM_SCOPE, scope);
    }
    DefaultAudience audience = request.getDefaultAudience();
    parameters.putString(ServerProtocol.DIALOG_PARAM_DEFAULT_AUDIENCE, audience.getNativeProtocolAudience());
    parameters.putString(ServerProtocol.DIALOG_PARAM_STATE, getClientState(request.getAuthId()));
    AccessToken previousToken = AccessToken.getCurrentAccessToken();
    String previousTokenString = previousToken != null ? previousToken.getToken() : null;
    if (previousTokenString != null && (previousTokenString.equals(loadCookieToken()))) {
        parameters.putString(ServerProtocol.DIALOG_PARAM_ACCESS_TOKEN, previousTokenString);
        // Don't log the actual access token, just its presence or absence.
        addLoggingExtra(ServerProtocol.DIALOG_PARAM_ACCESS_TOKEN, AppEventsConstants.EVENT_PARAM_VALUE_YES);
    } else {
        // The call to clear cookies will create the first instance of CookieSyncManager if
        // necessary
        Utility.clearFacebookCookies(loginClient.getActivity());
        addLoggingExtra(ServerProtocol.DIALOG_PARAM_ACCESS_TOKEN, AppEventsConstants.EVENT_PARAM_VALUE_NO);
    }
    return parameters;
}
Also used : Bundle(android.os.Bundle) AccessToken(com.facebook.AccessToken)

Example 28 with AccessToken

use of com.facebook.AccessToken in project facebook-android-sdk by facebook.

the class LoginManager method onActivityResult.

boolean onActivityResult(int resultCode, Intent data, FacebookCallback<LoginResult> callback) {
    FacebookException exception = null;
    AccessToken newToken = null;
    LoginClient.Result.Code code = LoginClient.Result.Code.ERROR;
    Map<String, String> loggingExtras = null;
    LoginClient.Request originalRequest = null;
    boolean isCanceled = false;
    if (data != null) {
        LoginClient.Result result = data.getParcelableExtra(LoginFragment.RESULT_KEY);
        if (result != null) {
            originalRequest = result.request;
            code = result.code;
            if (resultCode == Activity.RESULT_OK) {
                if (result.code == LoginClient.Result.Code.SUCCESS) {
                    newToken = result.token;
                } else {
                    exception = new FacebookAuthorizationException(result.errorMessage);
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                isCanceled = true;
            }
            loggingExtras = result.loggingExtras;
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        isCanceled = true;
        code = LoginClient.Result.Code.CANCEL;
    }
    if (exception == null && newToken == null && !isCanceled) {
        exception = new FacebookException("Unexpected call to LoginManager.onActivityResult");
    }
    boolean wasLoginActivityTried = true;
    //Sadly, there is no way to get activity context at this point.S
    Context context = null;
    logCompleteLogin(context, code, loggingExtras, exception, wasLoginActivityTried, originalRequest);
    finishLogin(newToken, originalRequest, exception, isCanceled, callback);
    return true;
}
Also used : FacebookAuthorizationException(com.facebook.FacebookAuthorizationException) Context(android.content.Context) FacebookException(com.facebook.FacebookException) AccessToken(com.facebook.AccessToken)

Example 29 with AccessToken

use of com.facebook.AccessToken in project facebook-android-sdk by facebook.

the class LoginManager method createLoginRequestFromResponse.

private LoginClient.Request createLoginRequestFromResponse(final GraphResponse response) {
    Validate.notNull(response, "response");
    AccessToken failedToken = response.getRequest().getAccessToken();
    return createLoginRequest(failedToken != null ? failedToken.getPermissions() : null);
}
Also used : AccessToken(com.facebook.AccessToken)

Example 30 with AccessToken

use of com.facebook.AccessToken in project facebook-android-sdk by facebook.

the class LoginMethodHandler method createAccessTokenFromNativeLogin.

static AccessToken createAccessTokenFromNativeLogin(Bundle bundle, AccessTokenSource source, String applicationId) {
    Date expires = Utility.getBundleLongAsDate(bundle, NativeProtocol.EXTRA_EXPIRES_SECONDS_SINCE_EPOCH, new Date(0));
    ArrayList<String> permissions = bundle.getStringArrayList(NativeProtocol.EXTRA_PERMISSIONS);
    String token = bundle.getString(NativeProtocol.EXTRA_ACCESS_TOKEN);
    if (Utility.isNullOrEmpty(token)) {
        return null;
    }
    String userId = bundle.getString(NativeProtocol.EXTRA_USER_ID);
    return new AccessToken(token, applicationId, userId, permissions, null, source, expires, new Date());
}
Also used : AccessToken(com.facebook.AccessToken) Date(java.util.Date)

Aggregations

AccessToken (com.facebook.AccessToken)49 Test (org.junit.Test)16 Bundle (android.os.Bundle)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 Date (java.util.Date)9 Intent (android.content.Intent)8 AccessTokenTracker (com.facebook.AccessTokenTracker)7 FacebookException (com.facebook.FacebookException)7 GraphRequest (com.facebook.GraphRequest)5 GraphResponse (com.facebook.GraphResponse)4 JSONObject (org.json.JSONObject)4 JSONException (org.json.JSONException)3 IntentFilter (android.content.IntentFilter)2 LocalBroadcastManager (android.support.v4.content.LocalBroadcastManager)2 LargeTest (android.test.suitebuilder.annotation.LargeTest)2 Profile (com.facebook.Profile)2 ProfileTracker (com.facebook.ProfileTracker)2 TestBlocker (com.facebook.TestBlocker)2 WaitForBroadcastReceiver (com.facebook.WaitForBroadcastReceiver)2 AppEventsLogger (com.facebook.appevents.AppEventsLogger)2