Search in sources :

Example 41 with AccessToken

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

the class GetTokenLoginMethodHandlerTest method testFromNativeLogin.

@Test
public void testFromNativeLogin() {
    ArrayList<String> permissions = Utility.arrayList("stream_publish", "go_outside_and_play");
    String token = "AnImaginaryTokenValue";
    String userId = "1000";
    long nowSeconds = new Date().getTime() / 1000;
    Intent intent = new Intent();
    intent.putExtra(NativeProtocol.EXTRA_ACCESS_TOKEN, token);
    intent.putExtra(NativeProtocol.EXTRA_EXPIRES_SECONDS_SINCE_EPOCH, nowSeconds + 60L);
    intent.putExtra(NativeProtocol.EXTRA_PERMISSIONS, permissions);
    intent.putExtra(NativeProtocol.EXTRA_USER_ID, userId);
    AccessToken accessToken = GetTokenLoginMethodHandler.createAccessTokenFromNativeLogin(intent.getExtras(), AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, "1234");
    TestUtils.assertSamePermissions(permissions, accessToken);
    assertEquals(token, accessToken.getToken());
    assertEquals(AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, accessToken.getSource());
    assertTrue(!accessToken.isExpired());
}
Also used : AccessToken(com.facebook.AccessToken) Intent(android.content.Intent) Date(java.util.Date) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 42 with AccessToken

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

the class KatanaProxyLoginMethodHandlerTest method testProxyAuthHandlesSuccess.

@Test
public void testProxyAuthHandlesSuccess() {
    Bundle bundle = new Bundle();
    bundle.putLong("expires_in", EXPIRES_IN_DELTA);
    bundle.putString("access_token", ACCESS_TOKEN);
    bundle.putString("signed_request", SIGNED_REQUEST_STR);
    Intent intent = new Intent();
    intent.putExtras(bundle);
    KatanaProxyLoginMethodHandler handler = new KatanaProxyLoginMethodHandler(mockLoginClient);
    LoginClient.Request request = createRequest();
    when(mockLoginClient.getPendingRequest()).thenReturn(request);
    handler.tryAuthorize(request);
    handler.onActivityResult(0, Activity.RESULT_OK, intent);
    ArgumentCaptor<LoginClient.Result> resultArgumentCaptor = ArgumentCaptor.forClass(LoginClient.Result.class);
    verify(mockLoginClient, times(1)).completeAndValidate(resultArgumentCaptor.capture());
    LoginClient.Result result = resultArgumentCaptor.getValue();
    assertNotNull(result);
    assertEquals(LoginClient.Result.Code.SUCCESS, result.code);
    AccessToken token = result.token;
    assertNotNull(token);
    assertEquals(ACCESS_TOKEN, token.getToken());
    assertDateDiffersWithinDelta(new Date(), token.getExpires(), EXPIRES_IN_DELTA * 1000, 1000);
    TestUtils.assertSamePermissions(PERMISSIONS, token.getPermissions());
}
Also used : Bundle(android.os.Bundle) AccessToken(com.facebook.AccessToken) Intent(android.content.Intent) Date(java.util.Date) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 43 with AccessToken

use of com.facebook.AccessToken in project react-native-fbsdk by facebook.

the class RCTLoginButton method init.

public void init() {
    mAccessTokenTracker = new AccessTokenTracker() {

        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
            if (currentAccessToken == null) {
                WritableMap event = Arguments.createMap();
                event.putString("type", "logoutFinished");
                ReactContext context = (ReactContext) getContext();
                context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
            }
        }
    };
    this.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            WritableMap event = Arguments.createMap();
            event.putString("type", "loginFinished");
            event.putString("error", null);
            WritableMap result = Arguments.createMap();
            result.putBoolean("isCancelled", false);
            result.putArray("grantedPermissions", Arguments.fromJavaArgs(setToStringArray(loginResult.getRecentlyGrantedPermissions())));
            result.putArray("declinedPermissions", Arguments.fromJavaArgs(setToStringArray(loginResult.getRecentlyDeniedPermissions())));
            event.putMap("result", result);
            ReactContext context = (ReactContext) getContext();
            context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
        }

        @Override
        public void onError(FacebookException error) {
            WritableMap event = Arguments.createMap();
            event.putString("type", "loginFinished");
            event.putString("error", error.toString());
            WritableMap result = Arguments.createMap();
            result.putBoolean("isCancelled", false);
            event.putMap("result", result);
            ReactContext context = (ReactContext) getContext();
            context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
        }

        @Override
        public void onCancel() {
            WritableMap event = Arguments.createMap();
            event.putString("type", "loginFinished");
            event.putString("error", null);
            WritableMap result = Arguments.createMap();
            result.putBoolean("isCancelled", true);
            event.putMap("result", result);
            ReactContext context = (ReactContext) getContext();
            context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
        }
    });
}
Also used : AccessTokenTracker(com.facebook.AccessTokenTracker) WritableMap(com.facebook.react.bridge.WritableMap) ThemedReactContext(com.facebook.react.uimanager.ThemedReactContext) ReactContext(com.facebook.react.bridge.ReactContext) AccessToken(com.facebook.AccessToken) FacebookException(com.facebook.FacebookException) LoginResult(com.facebook.login.LoginResult)

Example 44 with AccessToken

use of com.facebook.AccessToken in project react-native-fbsdk by facebook.

the class Utility method buildAccessToken.

public static AccessToken buildAccessToken(ReadableMap accessTokenMap) {
    AccessTokenSource accessTokenSource = AccessTokenSource.valueOf(accessTokenMap.getString("accessTokenSource"));
    Date expirationTime = new Date((long) accessTokenMap.getDouble("expirationTime"));
    Date lastRefreshTime = new Date((long) accessTokenMap.getDouble("lastRefreshTime"));
    return new AccessToken(accessTokenMap.getString("accessToken"), accessTokenMap.getString("applicationID"), accessTokenMap.getString("userID"), reactArrayToStringList(accessTokenMap.getArray("permissions")), reactArrayToStringList(accessTokenMap.getArray("declinedPermissions")), accessTokenSource, expirationTime, lastRefreshTime);
}
Also used : AccessTokenSource(com.facebook.AccessTokenSource) AccessToken(com.facebook.AccessToken) Date(java.util.Date)

Example 45 with AccessToken

use of com.facebook.AccessToken in project react-native-fbsdk by facebook.

the class FBAccessTokenModule method setCurrentAccessToken.

/**
     * Set {@link AccessToken} for the current session.
     * @param accessTokenMap must satisfy the requirements in
     *                       <a href="https://developers.facebook.com/docs/reference/android/current/class/AccessToken/">
     *                       Facebook AccessToken</a>
     */
@ReactMethod
public void setCurrentAccessToken(ReadableMap accessTokenMap) {
    AccessToken accessToken = Utility.buildAccessToken(accessTokenMap);
    AccessToken.setCurrentAccessToken(accessToken);
}
Also used : AccessToken(com.facebook.AccessToken) ReactMethod(com.facebook.react.bridge.ReactMethod)

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