Search in sources :

Example 21 with FacebookException

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

the class LoginMethodHandler method getUserIDFromSignedRequest.

private static String getUserIDFromSignedRequest(String signedRequest) throws FacebookException {
    if (signedRequest == null || signedRequest.isEmpty()) {
        throw new FacebookException("Authorization response does not contain the signed_request");
    }
    try {
        String[] signatureAndPayload = signedRequest.split("\\.");
        if (signatureAndPayload.length == 2) {
            byte[] data = Base64.decode(signatureAndPayload[1], Base64.DEFAULT);
            String dataStr = new String(data, "UTF-8");
            JSONObject jsonObject = new JSONObject(dataStr);
            return jsonObject.getString("user_id");
        }
    } catch (UnsupportedEncodingException ex) {
    } catch (JSONException ex) {
    }
    throw new FacebookException("Failed to retrieve user_id from signed_request");
}
Also used : JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException)

Example 22 with FacebookException

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

the class WebViewLoginMethodHandler method tryAuthorize.

@Override
boolean tryAuthorize(final LoginClient.Request request) {
    Bundle parameters = getParameters(request);
    WebDialog.OnCompleteListener listener = new WebDialog.OnCompleteListener() {

        @Override
        public void onComplete(Bundle values, FacebookException error) {
            onWebDialogComplete(request, values, error);
        }
    };
    e2e = LoginClient.getE2E();
    addLoggingExtra(ServerProtocol.DIALOG_PARAM_E2E, e2e);
    FragmentActivity fragmentActivity = loginClient.getActivity();
    WebDialog.Builder builder = new AuthDialogBuilder(fragmentActivity, request.getApplicationId(), parameters).setE2E(e2e).setIsRerequest(request.isRerequest()).setOnCompleteListener(listener);
    loginDialog = builder.build();
    FacebookDialogFragment dialogFragment = new FacebookDialogFragment();
    dialogFragment.setRetainInstance(true);
    dialogFragment.setDialog(loginDialog);
    dialogFragment.show(fragmentActivity.getSupportFragmentManager(), FacebookDialogFragment.TAG);
    return true;
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) WebDialog(com.facebook.internal.WebDialog) FacebookDialogFragment(com.facebook.internal.FacebookDialogFragment)

Example 23 with FacebookException

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

the class LoginManager method startLogin.

private void startLogin(StartActivityDelegate startActivityDelegate, LoginClient.Request request) throws FacebookException {
    logStartLogin(startActivityDelegate.getActivityContext(), request);
    // Make sure the static handler for login is registered if there isn't an explicit callback
    CallbackManagerImpl.registerStaticCallback(CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode(), new CallbackManagerImpl.Callback() {

        @Override
        public boolean onActivityResult(int resultCode, Intent data) {
            return LoginManager.this.onActivityResult(resultCode, data);
        }
    });
    boolean started = tryFacebookActivity(startActivityDelegate, request);
    if (!started) {
        FacebookException exception = new FacebookException("Log in attempt failed: FacebookActivity could not be started." + " Please make sure you added FacebookActivity to the AndroidManifest.");
        boolean wasLoginActivityTried = false;
        logCompleteLogin(startActivityDelegate.getActivityContext(), LoginClient.Result.Code.ERROR, null, exception, wasLoginActivityTried, request);
        throw exception;
    }
}
Also used : FacebookException(com.facebook.FacebookException) CallbackManagerImpl(com.facebook.internal.CallbackManagerImpl) Intent(android.content.Intent)

Example 24 with FacebookException

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

the class Utility method getStringPropertyAsJSON.

// Returns either a JSONObject or JSONArray representation of the 'key' property of
// 'jsonObject'.
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException {
    Object value = jsonObject.opt(key);
    if (value != null && value instanceof String) {
        JSONTokener tokener = new JSONTokener((String) value);
        value = tokener.nextValue();
    }
    if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) {
        if (nonJSONPropertyKey != null) {
            // Facebook sometimes gives us back a non-JSON value such as
            // literal "true" or "false" as a result.
            // If we got something like that, we present it to the caller as a JSONObject
            // with a single property. We only do this if the caller wants that behavior.
            jsonObject = new JSONObject();
            jsonObject.putOpt(nonJSONPropertyKey, value);
            return jsonObject;
        } else {
            throw new FacebookException("Got an unexpected non-JSON object.");
        }
    }
    return value;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 25 with FacebookException

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

the class SplashFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.splash, container, false);
    callbackManager = CallbackManager.Factory.create();
    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getActivity(), "Login successful", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(), "Login canceled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(), "Login error", Toast.LENGTH_SHORT).show();
        }
    });
    skipLoginButton = (TextView) view.findViewById(R.id.skip_login_button);
    skipLoginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (skipLoginCallback != null) {
                skipLoginCallback.onSkipLoginPressed();
            }
        }
    });
    return view;
}
Also used : FacebookException(com.facebook.FacebookException) LoginResult(com.facebook.login.LoginResult) TextView(android.widget.TextView) View(android.view.View)

Aggregations

FacebookException (com.facebook.FacebookException)70 Bundle (android.os.Bundle)24 JSONObject (org.json.JSONObject)24 JSONException (org.json.JSONException)19 Bitmap (android.graphics.Bitmap)13 GraphRequest (com.facebook.GraphRequest)10 JSONArray (org.json.JSONArray)10 Intent (android.content.Intent)9 Uri (android.net.Uri)8 GraphResponse (com.facebook.GraphResponse)8 URISyntaxException (java.net.URISyntaxException)8 AccessToken (com.facebook.AccessToken)7 FacebookOperationCanceledException (com.facebook.FacebookOperationCanceledException)5 FacebookRequestError (com.facebook.FacebookRequestError)5 LoginResult (com.facebook.login.LoginResult)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5