Search in sources :

Example 6 with AccessToken

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

the class LoginClientTests method testReauthorizationWithDifferentFbidsFails.

@LargeTest
public void testReauthorizationWithDifferentFbidsFails() throws Exception {
    TestBlocker blocker = getTestBlocker();
    MockValidatingLoginClient client = new MockValidatingLoginClient(null, blocker);
    client.addAccessTokenToFbidMapping(USER_1_ACCESS_TOKEN, USER_1_FBID);
    client.addAccessTokenToFbidMapping(USER_2_ACCESS_TOKEN, USER_2_FBID);
    client.setPermissionsToReport(PERMISSIONS);
    LoginClient.Request request = createNewPermissionRequest();
    client.setRequest(request);
    AccessToken userOneToken = new AccessToken(USER_1_ACCESS_TOKEN, APP_ID, USER_1_FBID, PERMISSIONS, null, null, null, null);
    AccessToken.setCurrentAccessToken(userOneToken);
    AccessToken userTwoToken = new AccessToken(USER_2_ACCESS_TOKEN, APP_ID, USER_2_FBID, PERMISSIONS, null, null, null, null);
    LoginClient.Result result = LoginClient.Result.createTokenResult(request, userTwoToken);
    client.completeAndValidate(result);
    blocker.waitForSignals(1);
    assertNotNull(client.result);
    assertEquals(LoginClient.Result.Code.ERROR, client.result.code);
    assertNull(client.result.token);
    assertNotNull(client.result.errorMessage);
}
Also used : TestBlocker(com.facebook.TestBlocker) AccessToken(com.facebook.AccessToken) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 7 with AccessToken

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

the class DeviceAuthDialog method onSuccess.

private void onSuccess(final String accessToken) {
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,permissions,name");
    AccessToken temporaryToken = new AccessToken(accessToken, FacebookSdk.getApplicationId(), "0", null, null, null, null, null);
    GraphRequest request = new GraphRequest(temporaryToken, "me", parameters, HttpMethod.GET, new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            if (completed.get()) {
                return;
            }
            if (response.getError() != null) {
                onError(response.getError().getException());
                return;
            }
            String userId;
            Utility.PermissionsPair permissions;
            String name;
            try {
                JSONObject jsonObject = response.getJSONObject();
                userId = jsonObject.getString("id");
                permissions = Utility.handlePermissionResponse(jsonObject);
                name = jsonObject.getString("name");
            } catch (JSONException ex) {
                onError(new FacebookException(ex));
                return;
            }
            DeviceRequestsHelper.cleanUpAdvertisementService(currentRequestState.getUserCode());
            boolean requireConfirm = FetchedAppSettingsManager.getAppSettingsWithoutQuery(FacebookSdk.getApplicationId()).getSmartLoginOptions().contains(SmartLoginOption.RequireConfirm);
            if (requireConfirm && !isRetry) {
                isRetry = true;
                presentConfirmation(userId, permissions, accessToken, name);
                return;
            }
            completeLogin(userId, permissions, accessToken);
        }
    });
    request.executeAsync();
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) AccessToken(com.facebook.AccessToken) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 8 with AccessToken

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

the class LoginClient method validateSameFbidAndFinish.

void validateSameFbidAndFinish(Result pendingResult) {
    if (pendingResult.token == null) {
        throw new FacebookException("Can't validate without a token");
    }
    AccessToken previousToken = AccessToken.getCurrentAccessToken();
    AccessToken newToken = pendingResult.token;
    try {
        Result result;
        if (previousToken != null && newToken != null && previousToken.getUserId().equals(newToken.getUserId())) {
            result = Result.createTokenResult(pendingRequest, pendingResult.token);
        } 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()));
    }
}
Also used : FacebookException(com.facebook.FacebookException) AccessToken(com.facebook.AccessToken) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 9 with AccessToken

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

the class LoginButton method configureButton.

@Override
protected void configureButton(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
    super.configureButton(context, attrs, defStyleAttr, defStyleRes);
    setInternalOnClickListener(getNewLoginClickListener());
    parseLoginButtonAttributes(context, attrs, defStyleAttr, defStyleRes);
    if (isInEditMode()) {
        // cannot use a drawable in edit mode, so setting the background color instead
        // of a background resource.
        setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
        // hardcoding in edit mode as getResources().getString() doesn't seem to work in
        // IntelliJ
        loginText = "Continue with Facebook";
    } else {
        accessTokenTracker = new AccessTokenTracker() {

            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                setButtonText();
            }
        };
    }
    setButtonText();
    // set vector drawables on the button
    setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(getContext(), R.drawable.com_facebook_button_login_logo), null, null, null);
}
Also used : AccessTokenTracker(com.facebook.AccessTokenTracker) AccessToken(com.facebook.AccessToken)

Example 10 with AccessToken

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

the class LoginMethodHandler method createAccessTokenFromWebBundle.

public static AccessToken createAccessTokenFromWebBundle(Collection<String> requestedPermissions, Bundle bundle, AccessTokenSource source, String applicationId) throws FacebookException {
    Date expires = Utility.getBundleLongAsDate(bundle, AccessToken.EXPIRES_IN_KEY, new Date());
    String token = bundle.getString(AccessToken.ACCESS_TOKEN_KEY);
    // With Login v4, we now get back the actual permissions granted, so update the permissions
    // to be the real thing
    String grantedPermissions = bundle.getString("granted_scopes");
    if (!Utility.isNullOrEmpty(grantedPermissions)) {
        requestedPermissions = new ArrayList<String>(Arrays.asList(grantedPermissions.split(",")));
    }
    String deniedPermissions = bundle.getString("denied_scopes");
    List<String> declinedPermissions = null;
    if (!Utility.isNullOrEmpty(deniedPermissions)) {
        declinedPermissions = new ArrayList<String>(Arrays.asList(deniedPermissions.split(",")));
    }
    if (Utility.isNullOrEmpty(token)) {
        return null;
    }
    String signed_request = bundle.getString("signed_request");
    String userId = getUserIDFromSignedRequest(signed_request);
    return new AccessToken(token, applicationId, userId, requestedPermissions, declinedPermissions, 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