Search in sources :

Example 1 with AccessToken

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

the class AppEventsLoggerTests method testExplicitCallWithNoAppSettings.

public void testExplicitCallWithNoAppSettings() throws InterruptedException {
    AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.EXPLICIT_ONLY);
    AccessToken accessToken1 = getFakeAccessToken();
    FacebookSdk.setApplicationId("234");
    AppEventsLogger logger1 = AppEventsLogger.newLogger(getActivity(), accessToken1);
    final WaitForBroadcastReceiver waitForBroadcastReceiver = new WaitForBroadcastReceiver();
    waitForBroadcastReceiver.incrementExpectCount();
    final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
    try {
        // Need to get notifications on another thread so we can wait for them.
        runOnBlockerThread(new Runnable() {

            @Override
            public void run() {
                broadcastManager.registerReceiver(waitForBroadcastReceiver, new IntentFilter(AppEventsLogger.ACTION_APP_EVENTS_FLUSHED));
            }
        }, true);
        logger1.logEvent("an_event");
        logger1.flush();
        waitForBroadcastReceiver.waitForExpectedCalls();
        closeBlockerAndAssertSuccess();
    } finally {
        broadcastManager.unregisterReceiver(waitForBroadcastReceiver);
    }
}
Also used : IntentFilter(android.content.IntentFilter) WaitForBroadcastReceiver(com.facebook.WaitForBroadcastReceiver) AccessToken(com.facebook.AccessToken) AppEventsLogger(com.facebook.appevents.AppEventsLogger) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager)

Example 2 with AccessToken

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

the class AppEventsLoggerTests method testSimpleCall.

public void testSimpleCall() throws InterruptedException {
    AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.EXPLICIT_ONLY);
    AccessToken accessToken1 = getAccessTokenForSharedUser();
    AccessToken accessToken2 = getAccessTokenForSharedUser(SECOND_TEST_USER_TAG);
    AppEventsLogger logger1 = AppEventsLogger.newLogger(getActivity(), accessToken1);
    AppEventsLogger logger2 = AppEventsLogger.newLogger(getActivity(), accessToken2);
    final WaitForBroadcastReceiver waitForBroadcastReceiver = new WaitForBroadcastReceiver();
    waitForBroadcastReceiver.incrementExpectCount();
    final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
    try {
        // Need to get notifications on another thread so we can wait for them.
        runOnBlockerThread(new Runnable() {

            @Override
            public void run() {
                broadcastManager.registerReceiver(waitForBroadcastReceiver, new IntentFilter(AppEventsLogger.ACTION_APP_EVENTS_FLUSHED));
            }
        }, true);
        logger1.logEvent("an_event");
        logger2.logEvent("another_event");
        // test illegal event name and event key, should not crash in non-debug environment.
        logger1.logEvent("$illegal_event_name");
        Bundle params = new Bundle();
        params.putString("illegal%key", "good_value");
        logger1.logEvent("legal_event_name", params);
        char[] val = { 'b', 'a', 'd' };
        params.putCharArray("legal_key", val);
        logger1.logEvent("legal_event", params);
        logger1.flush();
        waitForBroadcastReceiver.waitForExpectedCalls();
        closeBlockerAndAssertSuccess();
    } finally {
        broadcastManager.unregisterReceiver(waitForBroadcastReceiver);
    }
}
Also used : IntentFilter(android.content.IntentFilter) WaitForBroadcastReceiver(com.facebook.WaitForBroadcastReceiver) AccessToken(com.facebook.AccessToken) Bundle(android.os.Bundle) AppEventsLogger(com.facebook.appevents.AppEventsLogger) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager)

Example 3 with AccessToken

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

the class ShareApi method sharePhotoContent.

private void sharePhotoContent(final SharePhotoContent photoContent, final FacebookCallback<Sharer.Result> callback) {
    final Mutable<Integer> requestCount = new Mutable<Integer>(0);
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    final ArrayList<GraphRequest> requests = new ArrayList<GraphRequest>();
    final ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    final ArrayList<GraphResponse> errorResponses = new ArrayList<GraphResponse>();
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject result = response.getJSONObject();
            if (result != null) {
                results.add(result);
            }
            if (response.getError() != null) {
                errorResponses.add(response);
            }
            requestCount.value -= 1;
            if (requestCount.value == 0) {
                if (!errorResponses.isEmpty()) {
                    ShareInternalUtility.invokeCallbackWithResults(callback, null, errorResponses.get(0));
                } else if (!results.isEmpty()) {
                    final String postId = results.get(0).optString("id");
                    ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
                }
            }
        }
    };
    try {
        for (SharePhoto photo : photoContent.getPhotos()) {
            Bundle params;
            try {
                params = getSharePhotoCommonParameters(photo, photoContent);
            } catch (JSONException e) {
                ShareInternalUtility.invokeCallbackWithException(callback, e);
                return;
            }
            final Bitmap bitmap = photo.getBitmap();
            final Uri photoUri = photo.getImageUrl();
            String caption = photo.getCaption();
            if (caption == null) {
                caption = this.getMessage();
            }
            if (bitmap != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), bitmap, caption, params, requestCallback));
            } else if (photoUri != null) {
                requests.add(GraphRequest.newUploadPhotoRequest(accessToken, getGraphPath(PHOTOS_EDGE), photoUri, caption, params, requestCallback));
            }
        }
        requestCount.value += requests.size();
        for (GraphRequest request : requests) {
            request.executeAsync();
        }
    } catch (final FileNotFoundException ex) {
        ShareInternalUtility.invokeCallbackWithException(callback, ex);
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) Uri(android.net.Uri) Mutable(com.facebook.internal.Mutable) Bitmap(android.graphics.Bitmap) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) AccessToken(com.facebook.AccessToken)

Example 4 with AccessToken

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

the class ShareDialog method canShowWebTypeCheck.

private static boolean canShowWebTypeCheck(Class<? extends ShareContent> contentType) {
    // If we don't have an instance of a ShareContent, then all we can do is check whether
    // this is a ShareLinkContent, which can be shared if configured properly.
    // The instance method version of this check is more accurate and should be used on
    // ShareDialog instances.
    // SharePhotoContent currently requires the user staging endpoint, so we need a user access
    // token, so we need to see if we have one
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    final boolean haveUserAccessToken = accessToken != null && !accessToken.isExpired();
    return ShareLinkContent.class.isAssignableFrom(contentType) || ShareOpenGraphContent.class.isAssignableFrom(contentType) || (SharePhotoContent.class.isAssignableFrom(contentType) && haveUserAccessToken);
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) AccessToken(com.facebook.AccessToken)

Example 5 with AccessToken

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

the class LoginClientTests method testReauthorizationWithSameFbidSucceeds.

@LargeTest
public void testReauthorizationWithSameFbidSucceeds() 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 token = new AccessToken(USER_1_ACCESS_TOKEN, APP_ID, USER_1_FBID, PERMISSIONS, null, null, null, null);
    AccessToken.setCurrentAccessToken(token);
    LoginClient.Result result = LoginClient.Result.createTokenResult(request, token);
    client.completeAndValidate(result);
    blocker.waitForSignals(1);
    assertNotNull(client.result);
    assertEquals(LoginClient.Result.Code.SUCCESS, client.result.code);
    AccessToken resultToken = client.result.token;
    assertNotNull(resultToken);
    assertEquals(USER_1_ACCESS_TOKEN, resultToken.getToken());
    // We don't care about ordering.
    assertEquals(new HashSet<String>(PERMISSIONS), new HashSet<String>(resultToken.getPermissions()));
}
Also used : TestBlocker(com.facebook.TestBlocker) AccessToken(com.facebook.AccessToken) LargeTest(android.test.suitebuilder.annotation.LargeTest)

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