Exemplo n.º 1
0
  /**
   * Full authorize method.
   *
   * <p>Starts either an Activity or a dialog which prompts the user to log in to Facebook and grant
   * the requested permissions to the given application.
   *
   * <p>This method will, when possible, use Facebook's single sign-on for Android to obtain an
   * access token. This involves proxying a call through the Facebook for Android stand-alone
   * application, which will handle the authentication flow, and return an OAuth access token for
   * making API calls.
   *
   * <p>Because this process will not be available for all users, if single sign-on is not possible,
   * this method will automatically fall back to the OAuth 2.0 User-Agent flow. In this flow, the
   * user credentials are handled by Facebook in an embedded WebView, not by the client application.
   * As such, the dialog makes a network request and renders HTML content rather than a native UI.
   * The access token is retrieved from a redirect to a special URL that the WebView handles.
   *
   * <p>Note that User credentials could be handled natively using the OAuth 2.0 Username and
   * Password Flow, but this is not supported by this SDK.
   *
   * <p>See http://developers.facebook.com/docs/authentication/ and http://wiki.oauth.net/OAuth-2
   * for more details.
   *
   * <p>Note that this method is asynchronous and the callback will be invoked in the original
   * calling thread (not in a background thread).
   *
   * <p>Also note that requests may be made to the API without calling authorize first, in which
   * case only public information is returned.
   *
   * <p>IMPORTANT: Note that single sign-on authentication will not function correctly if you do not
   * include a call to the authorizeCallback() method in your onActivityResult() function! Please
   * see below for more information. single sign-on may be disabled by passing FORCE_DIALOG_AUTH as
   * the activityCode parameter in your call to authorize().
   *
   * @param activity The Android activity in which we want to display the authorization dialog.
   * @param applicationId The Facebook application identifier e.g. "350685531728"
   * @param permissions A list of permissions required for this application: e.g. "read_stream",
   *     "publish_stream", "offline_access", etc. see
   *     http://developers.facebook.com/docs/authentication/permissions This parameter should not be
   *     null -- if you do not require any permissions, then pass in an empty String array.
   * @param activityCode Single sign-on requires an activity result to be called back to the client
   *     application -- if you are waiting on other activities to return data, pass a custom
   *     activity code here to avoid collisions. If you would like to force the use of legacy
   *     dialog-based authorization, pass FORCE_DIALOG_AUTH for this parameter. Otherwise just omit
   *     this parameter and Facebook will use a suitable default. See
   *     http://developer.android.com/reference/android/ app/Activity.html for more information.
   * @param listener Callback interface for notifying the calling application when the
   *     authentication dialog has completed, failed, or been canceled.
   */
  public void authorize(
      Activity activity, String[] permissions, int activityCode, final DialogListener listener) {

    boolean singleSignOnStarted = false;

    mAuthDialogListener = listener;

    // Prefer single sign-on, where available.
    if (activityCode >= 0) {
      singleSignOnStarted = startSingleSignOn(activity, mAppId, permissions, activityCode);
    }
    // Otherwise fall back to traditional dialog.
    if (!singleSignOnStarted) {
      startDialogAuth(activity, permissions);
    }
  }
  /**
   * Full authorize method.
   *
   * <p>Starts either an Activity or a dialog which prompts the user to log in to Facebook and grant
   * the requested permissions to the given application.
   *
   * <p>This method will, when possible, use Facebook's single sign-on for Android to obtain an
   * access token. This involves proxying a call through the Facebook for Android stand-alone
   * application, which will handle the authentication flow, and return an OAuth access token for
   * making API calls.
   *
   * <p>Because this process will not be available for all users, if single sign-on is not possible,
   * this method will automatically fall back to the OAuth 2.0 User-Agent flow. In this flow, the
   * user credentials are handled by Facebook in an embedded WebView, not by the client application.
   * As such, the dialog makes a network request and renders HTML content rather than a native UI.
   * The access token is retrieved from a redirect to a special URL that the WebView handles.
   *
   * <p>Note that User credentials could be handled natively using the OAuth 2.0 Username and
   * Password Flow, but this is not supported by this SDK.
   *
   * <p>See http://developers.facebook.com/docs/authentication/ and http://wiki.oauth.net/OAuth-2
   * for more details.
   *
   * <p>Note that this method is asynchronous and the callback will be invoked in the original
   * calling thread (not in a background thread).
   *
   * <p>Also note that requests may be made to the API without calling authorize first, in which
   * case only public information is returned.
   *
   * <p>IMPORTANT: Note that single sign-on authentication will not function correctly if you do not
   * include a call to the authorizeCallback() method in your onActivityResult() function! Please
   * see below for more information. single sign-on may be disabled by passing FORCE_DIALOG_AUTH as
   * the activityCode parameter in your call to authorize().
   *
   * @param activity The Android activity in which we want to display the authorization dialog.
   * @param applicationId The Facebook application identifier e.g. "350685531728"
   * @param permissions A list of permissions required for this application: e.g. "read_stream",
   *     "publish_stream", "offline_access", etc. see
   *     http://developers.facebook.com/docs/authentication/permissions This parameter should not be
   *     null -- if you do not require any permissions, then pass in an empty String array.
   * @param activityCode Single sign-on requires an activity result to be called back to the client
   *     application -- if you are waiting on other activities to return data, pass a custom
   *     activity code here to avoid collisions. If you would like to force the use of legacy
   *     dialog-based authorization, pass FORCE_DIALOG_AUTH for this parameter. Otherwise just omit
   *     this parameter and Facebook will use a suitable default. See
   *     http://developer.android.com/reference/android/ app/Activity.html for more information.
   * @param listener Callback interface for notifying the calling application when the
   *     authentication dialog has completed, failed, or been canceled.
   */
  public void authorize(
      Activity activity, String[] permissions, int activityCode, final DialogListener listener) {

    boolean singleSignOnStarted = false;

    mAuthDialogListener = listener;

    // fire off an auto-attribution publish if appropriate.
    autoPublishAsync(activity.getApplicationContext());

    // Prefer single sign-on, where available.
    if (activityCode >= 0) {
      singleSignOnStarted = startSingleSignOn(activity, mAppId, permissions, activityCode);
    }
    // Otherwise fall back to traditional dialog.
    if (!singleSignOnStarted) {
      startDialogAuth(activity, permissions);
    }
  }
Exemplo n.º 3
0
  /**
   * IMPORTANT: This method must be invoked at the top of the calling activity's onActivityResult()
   * function or Facebook authentication will not function properly!
   *
   * <p>If your calling activity does not currently implement onActivityResult(), you must implement
   * it and include a call to this method if you intend to use the authorize() method in this SDK.
   *
   * <p>For more information, see http://developer.android.com/reference/android/app/
   * Activity.html#onActivityResult(int, int, android.content.Intent)
   */
  public void authorizeCallback(int requestCode, int resultCode, Intent data) {
    if (requestCode == mAuthActivityCode) {

      // Successfully redirected.
      if (resultCode == Activity.RESULT_OK) {

        // Check OAuth 2.0/2.10 error code.
        String error = data.getStringExtra("error");
        if (error == null) {
          error = data.getStringExtra("error_type");
        }

        // A Facebook error occurred.
        if (error != null) {
          if (error.equals(SINGLE_SIGN_ON_DISABLED)
              || error.equals("AndroidAuthKillSwitchException")) {
            Log.d(
                "Facebook-authorize",
                "Hosted auth currently " + "disabled. Retrying dialog auth...");
            startDialogAuth(mAuthActivity, mAuthPermissions);
          } else if (error.equals("access_denied") || error.equals("OAuthAccessDeniedException")) {
            Log.d("Facebook-authorize", "Login canceled by user.");
            mAuthDialogListener.onCancel();
          } else {
            String description = data.getStringExtra("error_description");
            if (description != null) {
              error = error + ":" + description;
            }
            Log.d("Facebook-authorize", "Login failed: " + error);
            mAuthDialogListener.onFacebookError(new FacebookError(error));
          }

          // No errors.
        } else {
          setAccessToken(data.getStringExtra(TOKEN));
          setAccessExpiresIn(data.getStringExtra(EXPIRES));
          if (isSessionValid()) {
            Log.d(
                "Facebook-authorize",
                "Login Success! access_token="
                    + getAccessToken()
                    + " expires="
                    + getAccessExpires());
            mAuthDialogListener.onComplete(data.getExtras());
          } else {
            mAuthDialogListener.onFacebookError(
                new FacebookError("Failed to receive access token."));
          }
        }

        // An error occurred before we could be redirected.
      } else if (resultCode == Activity.RESULT_CANCELED) {

        // An Android error occured.
        if (data != null) {
          Log.d("Facebook-authorize", "Login failed: " + data.getStringExtra("error"));
          mAuthDialogListener.onError(
              new DialogError(
                  data.getStringExtra("error"),
                  data.getIntExtra("error_code", -1),
                  data.getStringExtra("failing_url")));

          // User pressed the 'back' button.
        } else {
          Log.d("Facebook-authorize", "Login canceled by user.");
          mAuthDialogListener.onCancel();
        }
      }
    }
  }